Java IO Streams

26
IO Streams IO Streams Java I/O is based on the notation of Java I/O is based on the notation of streams streams Streams are sequences of data Streams are sequences of data In a program, we read information from an In a program, we read information from an input input stream and write information to an output stream and write information to an output stream stream

Transcript of Java IO Streams

Page 1: Java IO Streams

IO StreamsIO Streams

Java I/O is based on the notation of Java I/O is based on the notation of streamsstreams

Streams are sequences of dataStreams are sequences of data

In a program, we read information from an input In a program, we read information from an input stream and write information to an output streamstream and write information to an output stream

Page 2: Java IO Streams

IO StreamsIO Streams

A stream can represent many different kinds ofA stream can represent many different kinds of sources and destinationssources and destinations – – disk files, devices, other programs, a disk files, devices, other programs, a

network socket, and memory arrays network socket, and memory arrays

Streams support many different kinds of dataStreams support many different kinds of data – – simple bytes, primitive data types, simple bytes, primitive data types,

localized characters, and objects localized characters, and objects

Page 3: Java IO Streams

Overview of I/O streamsOverview of I/O streams

The java.io package contains a collection of The java.io package contains a collection of stream classes that supports for reading and stream classes that supports for reading and writing.writing.

To use these classes, a program needs to import To use these classes, a program needs to import the package Java.io.the package Java.io.

Page 4: Java IO Streams

Overview of I/O streamsOverview of I/O streams

The stream classes are divided into two The stream classes are divided into two types, based on the data type types, based on the data type

Character Stream Character Stream Byte StreamByte Stream

Page 5: Java IO Streams

Character And Byte StreamsCharacter And Byte Streams

Byte streamsByte streams – – For binary dataFor binary data – – classes for byte streams:classes for byte streams: ● ● The The InputStream InputStream ClassClass ● ● The The OutputStream OutputStream ClassClass ● ● Both classes are Both classes are abstractabstract Character streamsCharacter streams – – For Unicode charactersFor Unicode characters – – classes for character streams:classes for character streams: ● ● The The Reader Reader classclass ● ● The The Writer Writer classclass ● ● Both classes are Both classes are abstractabstract

Page 6: Java IO Streams

StreamsStreams

JAVA distinguishes between 2 types of streams:JAVA distinguishes between 2 types of streams: Text – streams, containing ‘characters‘Text – streams, containing ‘characters‘

Binary Streams, containing 8 – bit informationBinary Streams, containing 8 – bit information

I ‘ M A S T R I N G \nProgram Device

Device11101101 0000000001101001Program

Page 7: Java IO Streams

Character StreamsCharacter Streams

Reader and Writer are the abstract super classes Reader and Writer are the abstract super classes for character streams in java.io. for character streams in java.io.

Reader provides the API and partial Reader provides the API and partial implementation for readers (streams that read implementation for readers (streams that read 16-bit characters)16-bit characters)

Writer provides the API and partial Writer provides the API and partial implementation for writers (streams that write 16-implementation for writers (streams that write 16-bit characters). bit characters).

Page 8: Java IO Streams

Character Streams.Character Streams. The following figure shows the class hierarchies for the The following figure shows the class hierarchies for the

Reader and Writer classes.Reader and Writer classes.

Page 9: Java IO Streams

Methods in Writer ClassMethods in Writer Class

Abstract void close()Abstract void close() closes the output stream. closes the output stream.

Abstract void flush()Abstract void flush() Finalizes the output state so that any Finalizes the output state so that any buffers are cleared. That is it flushes the output buffers.buffers are cleared. That is it flushes the output buffers.

Void write()Void write() Writes a single character to the invoking stream. Writes a single character to the invoking stream.

Void write(char buffer[])Void write(char buffer[]) Writes a complete array of Writes a complete array of character to the invoking stream.character to the invoking stream.

Void write(str)Void write(str) Writes a str to the invoking stream. Writes a str to the invoking stream.

Page 10: Java IO Streams

Methods in Reader ClassMethods in Reader Class

abstract Void close()abstract Void close() Closes the Input source and after Closes the Input source and after closing if we attempt to read it will give IOException.closing if we attempt to read it will give IOException.

Void mark()Void mark() Places the mark at the current point in the Places the mark at the current point in the input stream.input stream.

Int read()Int read() Returns an integer representing of the next Returns an integer representing of the next available character of input. Returns -1 if EOF encountered.available character of input. Returns -1 if EOF encountered.

Void reset()Void reset() Resets the input pointer to the previously set Resets the input pointer to the previously set mark.mark.

Boolean ready()Boolean ready() Returns True if the next input request will Returns True if the next input request will not wait. Otherwise, it returns False.not wait. Otherwise, it returns False.

Page 11: Java IO Streams

Classes in character StreamsClasses in character Streams FileReaderFileReader This class creates a Reader that you can use This class creates a Reader that you can use

to read the contents of the file. The constructors in this to read the contents of the file. The constructors in this class is as follows.,class is as follows.,

FileReader(String filepath)FileReader(String filepath)

FileReader(File fileobj)FileReader(File fileobj)

FileWriterFileWriter This class creates a Writer that you can use to This class creates a Writer that you can use to write the contents of the file. The constructors in this class write the contents of the file. The constructors in this class is as follows.,is as follows.,

FileWriter(String filepath)FileWriter(String filepath)

FileWriter(File fileobj)FileWriter(File fileobj)

Page 12: Java IO Streams

Classes in character StreamsClasses in character Streams CharArrayReaderCharArrayReader This class is an implementation of an This class is an implementation of an

input stream that uses the char array as source. input stream that uses the char array as source. This class has two constructors, each of which requires a character array to provide the data source,

CharArrayReader(char array[ ]) CharArrayReader(char array[ ], int start, int numChars)

CharArrayWriterCharArrayWriter This class is an implementation of an This class is an implementation of an output stream that uses the char array as destination. This output stream that uses the char array as destination. This class has two constructors,class has two constructors,

CharArrayWriter(Char array[])CharArrayWriter(Char array[])

CharArrayWriter(int numChars)CharArrayWriter(int numChars)

Page 13: Java IO Streams

Classes in character StreamsClasses in character Streams

Buffered ReaderBuffered Reader

Streams are wrapped to combine the Streams are wrapped to combine the various features of the many streams. various features of the many streams.

The constructors in this class are The constructors in this class are BufferedWriter(Reader inputStream)BufferedWriter(Reader inputStream)

BufferedWriter(Reader inputStream, int bufSize)BufferedWriter(Reader inputStream, int bufSize)

Page 14: Java IO Streams

Classes in character StreamsClasses in character Streams

Buffered WriterBuffered Writer

The writer that adds the flush() method that can be used to The writer that adds the flush() method that can be used to ensure that data buffers are physically written to the actual ensure that data buffers are physically written to the actual output streamoutput stream

Using BufferWriter we can increase performance by Using BufferWriter we can increase performance by reducing the number of times data is actually physically reducing the number of times data is actually physically written to the output stream.written to the output stream.

The constructors in this class are The constructors in this class are

BufferedWriter(Writer outputStream)BufferedWriter(Writer outputStream)

BufferedWriter(Writer outputStream, int bufSize)BufferedWriter(Writer outputStream, int bufSize)

Page 15: Java IO Streams

Byte Streams.Byte Streams.

To read and write 8-bit bytes, programs should To read and write 8-bit bytes, programs should use the byte streams, descendents of use the byte streams, descendents of InputStream and OutputStream . InputStream and OutputStream .

InputStream and OutputStream provide the API InputStream and OutputStream provide the API and partial implementation for input streams and partial implementation for input streams (streams that read 8-bit bytes) and output (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). streams (streams that write 8-bit bytes).

Page 16: Java IO Streams

Byte Streams.Byte Streams.

These streams are typically used to read and write binary These streams are typically used to read and write binary data such as images and sounds. data such as images and sounds.

Two of the byte stream classes, Two of the byte stream classes,

1.ObjectInputStream 1.ObjectInputStream

2.ObjectOutputStream2.ObjectOutputStream

are used for object serialization.are used for object serialization.

Page 17: Java IO Streams

Byte StreamsByte Streams

The class hierarchy for the Reader ClassThe class hierarchy for the Reader Class

Page 18: Java IO Streams

Methods In Input Stream(Byte)Methods In Input Stream(Byte)

Int available()Int available() Returns the number of bytes of input Returns the number of bytes of input currently available for reading.currently available for reading.

Void close()Void close() Closes the Input source and after Closes the Input source and after closing if we attempt to read it will give IOException.closing if we attempt to read it will give IOException.

Int read()Int read() Returns an integer representing of the Returns an integer representing of the next byte of input. Returns -1 if EOF encounters.next byte of input. Returns -1 if EOF encounters.

Void reset()Void reset() Resets the input pointer to the Resets the input pointer to the previously set mark.previously set mark.

Page 19: Java IO Streams

Byte StreamByte Stream

Class hierarchy figure for Writer ClassClass hierarchy figure for Writer Class

Page 20: Java IO Streams

Methods In Output StreamMethods In Output Stream

Void close()Void close() Closes the Output Stream. Closes the Output Stream.

Void flush()Void flush() Finalizes the output state so that any Finalizes the output state so that any buffers are cleared, it flushes the output buffers.buffers are cleared, it flushes the output buffers.

Void write(int b)Void write(int b) Writes a single byte to an output Writes a single byte to an output Stream.Stream.

Page 21: Java IO Streams

File StreamsFile Streams The File streamsThe File streams

1.1.FileInputStream FileInputStream This creates an InputStream that we can use to read This creates an InputStream that we can use to read

bytes from a file. Two constructors are there, bytes from a file. Two constructors are there, FileInputStream(String filepath)FileInputStream(String filepath) FileInputStream(File fileobj)FileInputStream(File fileobj)

2.2.FileOutputStreamFileOutputStream This creates an OutputStream that we can use to Write This creates an OutputStream that we can use to Write

bytes to a file. Four constructors are there, bytes to a file. Four constructors are there, FileOutputStream(String filepath)FileOutputStream(String filepath) FileOutputStream(File fileobj)FileOutputStream(File fileobj) FileOutputStream(String filepath, boolean append)FileOutputStream(String filepath, boolean append) FileOutputStream(File fileobj, boolean append)FileOutputStream(File fileobj, boolean append)

Page 22: Java IO Streams

ByteArrayInputStreamByteArrayInputStream

Is an implementation of an input stream that uses the Is an implementation of an input stream that uses the byte array as the source.byte array as the source.

There are two constructorsThere are two constructors

ByteArrayInputStream(ByteArrayInputStream(byte array[]byte array[]))

ByteArrayInputStream(ByteArrayInputStream(byte array[], int start, int numBytesbyte array[], int start, int numBytes))

Page 23: Java IO Streams

ByteArrayOutputStreamByteArrayOutputStream

Is an implementation of an output stream that uses the Is an implementation of an output stream that uses the byte array as the destination.byte array as the destination.

There are two constructorsThere are two constructors

ByteArrayOutputStream()ByteArrayOutputStream()

ByteArrayOutputStream(ByteArrayOutputStream( int numBytes int numBytes))

Page 24: Java IO Streams

Pipe StreamsPipe Streams

Pipes are used to channel the output from Pipes are used to channel the output from one thread into the input of another.one thread into the input of another.

PipedReader and PipedWriter implement the PipedReader and PipedWriter implement the input and output components of a pipeinput and output components of a pipe

Page 25: Java IO Streams

Working with Filter StreamsWorking with Filter Streams

The java.io package provides a set of The java.io package provides a set of abstract classes that define and abstract classes that define and partially implement partially implement filter streamsfilter streams. A . A filter stream filters data as it's being filter stream filters data as it's being read from or written to the stream. read from or written to the stream.

The filter streams are The filter streams are 1.FilterInputStream 1.FilterInputStream 2.FilterOutputStream 2.FilterOutputStream

Page 26: Java IO Streams

Object SerializationObject Serialization

Two stream classes in Two stream classes in java.iojava.io,, 1. 1. ObjectInputStreamObjectInputStream 2. 2. ObjectOutputStreamObjectOutputStream are used to read and write objects. are used to read and write objects.

The key to writing an object is to represent its state in The key to writing an object is to represent its state in a serialized form sufficient to reconstruct the object as a serialized form sufficient to reconstruct the object as it is read. This process is called object serialization. it is read. This process is called object serialization.