Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
Standard input, output and error. Lecture Under Construction.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Streams and Files The objectives of this chapter are: To understand the principles of I/O streams and where to use them To understand the options and limitations.
Java I/O and Java Networking (Client Side) Yoshi.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Richiami di Java Input/Output. import java.io.*; public class Simple { public static void main(String a[]){ new Simple(); } public Simple() { byte buffer[]=new.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Two Ways to Store Data in a File  Text format  Binary format.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
1 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
Java Programming Language (3) - Input/Output Stream –
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
The Java IO System Different kinds of IO Different kinds of operations
CSC1351 Class 6 Classes & Inheritance.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
Ch14 Files and Streams OBJECTIVES
Chapter 8: Input and Output
Object Writing in files
I/O Basics.
Java Programming Course
JAVA IO.
Stream Oriented I/O Computer Programming II Dr. Tim Margush
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
Java Basics Introduction to Streams.
14 Files and Streams.
Presentation transcript:

Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture

Background Java’s I/O model is defined in terms of streams—ordered sequences of data with a source or destination. Initially, these were (8-bit) byte streams like cin, cout, and cerr for C++. The corresponding byte streams for Java are: –System.in (an InputStream) –System.out (a PrintStream) –System.err (an unbuffered PrintStream) Later, Unicode character streams were added. These are called Readers and Writers. These classes and interfaces are found in java.io.* This is only a short introduction to a long subject! Read the text! Test questions on this material!

Stacking Streams The important concept in working with Java I/O is stacking streams. That is, you apply adaptors to a stream to specialise it for a specific task. For example, a File can be used via a FileStream. A.gz file can be used via a GZIPInputStream.

Some Byte Stream Operations java.io.InputStream (base class for byte input) –int read()—reads a byte –int read(byte[])—reads an array of bytes –void close()—closes the stream java.io.OutputStream (base class for byte output) –void write(int)—writes a byte passed as int –void write(byte[])—writes an array –void flush()—flushes the stream –void close()—closes the stream

Some Character Stream Operations java.io.Reader –int read()—reads a Unicode character –int read(char[])—reads a character array –boolean ready()—returns true if there is data to read –void close() java.io.Writer –void write(int)—writes a character –void write(char[]) –void write(String)—writes a String –void flush() –void close()

Formatted Input/Output DataInput and DataOutput are interfaces used to transfer data other than bytes or characters (e.g., binary data). Implemented by: –DataInputStream—readType() –DataOutputStream—writeType() –RandomAccessFile—implements both and allows positioning in the file. where Type includes all the primitive types. readLine(), writeBytes(String), writeChars(String), and writeUTF(String) are used for String data.

Data Conversion The wrapper classes for the primitive types (e.g, Double for double) provide static methods for data conversion from a String to that type. All classes also define a toString() method to convert the class data to a String. This is used in println() and when Strings are constructed using the + and += operators. If you want this method to be useful, you should override the default method inherited from Object. Test questions!

Various InputStreams FileInputStream—built around a file. PipedInputStream—PipedInputStreams are connected to PipedOutputStreams and are used between threads. ByteArrayInputStream—Has an internal byte array provided by the creator of the stream and where the data are stored. StringBufferInputStream—Like a ByteArrayInputStream. The internal buffer is a String. SequenceInputStream—Reads from multiple streams, finishing one before going on to the next.

More InputStreams FilterInputStream—Generally built around some other InputStream, using it as a source of data and doing further conversions. This ‘stacking’ can involve multiple filters. Examples include: –BufferedInputStream—Buffers the input and supports mark and reset operations. –DataInputStream—Formatted input operations. –LineNumberInputStream—Reads one line at a time, keeping count –PushBackInputStream—Allows the last byte to be ‘unread’ if desired. Useful for user interfaces.

Various OutputStreams PrintStream—provides print(arg) and println(arg) operations to output data in a readable form. Both operate by applying the toString() method to arg and then printing the resulting String. println() outputs an end of line as well. File-, Piped-, ByteArray-, Filter-, Buffered-, and DataOutputStream work generally as you would be expect.

Unicode Character Streams BufferedReader, BufferedWriter, LineNumberReader CharArrayReader, CharArrayWriter FilterReader, PushBackReader, FilterWriter InputStreamReader, OutputStreamWriter, FileReader, FileWriter PipedReader, PipedWriter StringReader, StringWriter PrintWriter

Files and Directories An instance of class File represents the pathname (a String) to some file or directory. File creation and deletion are handled through the File class. There are some problems having to do with pathnames for files in Java. No bugs; just a collection of surprises.

File Input/Output A FileInputStream or FileOutputStream is built around a pathname or a File. Otherwise, it is basically a Stream of the appropriate type. You can build a FilterInputStream or a FilterOutputStream around these streams.

Data Compression Specialized filter streams exist to operate on data. E.g., java.util.zip.GZIPOutputStream if created around another OutputStream will apply gzip compression to the data. You can also use other classes in the java.util package to generate a checksum for the stream. The class java.util.zip.ZipFile allows you to access a ZIP archive and read the entries through a stream.

Interfacing to Arrays The ByteArrayXXX, StringReader/ StringWriter and CharArrayXXX streams allow you to read and write data stored in byte arrays and character strings.

Serialization This is the capacity to serialize objects, sending them over a stream and deserializing them at the other end to reconstruct the object. This can also be used to save objects offline. java.io.ObjectOutputStream and java.io.ObjectInputStream participate in this. Methods used: writeObject() and readObject(). Used in: –Remote Method Invocation (RMI) –Handling persistent objects –Distributed agent networks

Networking In the java.net.* package. Supports TCP and UDP protocols. URL class is used to represent a Uniform Resource Locator Also supports sockets/ports with the Socket class. I have watched one of my students write a working IRC client in Java in less than 15 minutes. Secure Socket Layer is available in Java 1.4.

Logging New in Java 1.4 In the java.util.logging.* package. Automates the creation of a log with multiple severity thresholds. Can be used to create an audit trail for security.

Non-Blocking I/O New for Java 1.4 In java.nio.* Traditionally, Java I/O was blocking, with the thread having to wait for its data. This is non-blocking I/O. Supports high-performance I/O.

XML New for Java 1.4 Supports XML documents and document formats. SAXP, JAXP, and XDOC. Used in JavaServer Pages. If you care about XML, you care a lot.