100% found this document useful (5 votes)
3 views26 pages

Java IO Streams

Java I / O is based on the notation of streams streams are sequences of data. A stream can represent many different kinds of sources and destinations. To use these classes, a program needs to import the package Java.io.

Uploaded by

S R Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
3 views26 pages

Java IO Streams

Java I / O is based on the notation of streams streams are sequences of data. A stream can represent many different kinds of sources and destinations. To use these classes, a program needs to import the package Java.io.

Uploaded by

S R Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

IO Streams

 Java I/O is based on the notation of streams

 Streams are sequences of data

 In a program, we read information from an input

stream and write information to an output stream


IO Streams

 A stream can represent many different kinds of


sources and destinations
– disk files, devices, other programs, a
network socket, and memory arrays

 Streams support many different kinds of data


– simple bytes, primitive data types,
localized characters, and objects
Overview of I/O streams

 The [Link] package contains a collection of


stream classes that supports for reading and
writing.

 To use these classes, a program needs to


import the package [Link].
Overview of I/O streams

 The stream classes are divided into two


types, based on the data type

 Character Stream
 Byte Stream
Character And Byte Streams
 Byte streams
– For binary data
– classes for byte streams:
● The InputStream Class
● The OutputStream Class
● Both classes are abstract
 Character streams
– For Unicode characters
– classes for character streams:
● The Reader class
● The Writer class
● Both classes are abstract
Streams

 JAVA distinguishes between 2 types of streams:


 Text – streams, containing ‘characters‘

Program I ‘ M A S T R I N G \n Device

 Binary Streams, containing 8 – bit information

Program 01101001 11101101 00000000 Device


Character Streams

 Reader and Writer are the abstract super classes


for character streams in [Link].

 Reader provides the API and partial


implementation for readers (streams that read
16-bit characters)

 Writer provides the API and partial


implementation for writers (streams that write
16-bit characters).
Character Streams.
 The following figure shows the class hierarchies for the
Reader and Writer classes.
Methods in Writer Class
 Abstract void close() closes the output stream.

 Abstract void flush() Finalizes the output state so that any


buffers are cleared. That is it flushes the output buffers.

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

 Void write(char buffer[]) Writes a complete array of


character to the invoking stream.

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


Methods in Reader Class

 abstract Void close() Closes the Input source and after


closing if we attempt to read it will give IOException.
 Void mark() Places the mark at the current point in the
input stream.
 Int read() Returns an integer representing of the next
available character of input. Returns -1 if EOF encountered.
 Void reset() Resets the input pointer to the previously set
mark.
 Boolean ready() Returns True if the next input request
will not wait. Otherwise, it returns False.
Classes in character Streams
 FileReader This class creates a Reader that you can use
to read the contents of the file. The constructors in this
class is as follows.,
FileReader(String filepath)
FileReader(File fileobj)

 FileWriter This class creates a Writer that you can use to


write the contents of the file. The constructors in this class
is as follows.,
FileWriter(String filepath)
FileWriter(File fileobj)
Classes in character Streams
 CharArrayReader This class is an implementation of an
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)

 CharArrayWriter This class is an implementation of an


output stream that uses the char array as destination. This
class has two constructors,
CharArrayWriter(Char array[])
CharArrayWriter(int numChars)
Classes in character Streams
Buffered Reader 

 Streams are wrapped to combine the


various features of the many streams.

 The constructors in this class are


BufferedWriter(Reader inputStream)
BufferedWriter(Reader inputStream, int bufSize)
Classes in character Streams
Buffered Writer

 The writer that adds the flush() method that can be used to
ensure that data buffers are physically written to the actual
output stream
 Using BufferWriter we can increase performance by
reducing the number of times data is actually physically
written to the output stream.
 The constructors in this class are
BufferedWriter(Writer outputStream)
BufferedWriter(Writer outputStream, int bufSize)
Byte Streams.

 To read and write 8-bit bytes, programs should


use the byte streams, descendents of
InputStream and OutputStream .

 InputStream and OutputStream provide the API


and partial implementation for input streams
(streams that read 8-bit bytes) and output
streams (streams that write 8-bit bytes).
Byte Streams.

 These streams are typically used to read and write binary


data such as images and sounds.

 Two of the byte stream classes,


[Link]
[Link]

are used for object serialization.


Byte Streams
 The class hierarchy for the Reader Class
Methods In Input Stream(Byte)

 Int available() Returns the number of bytes of input


currently available for reading.

 Void close() Closes the Input source and after


closing if we attempt to read it will give IOException.

 Int read() Returns an integer representing of the


next byte of input. Returns -1 if EOF encounters.

 Void reset() Resets the input pointer to the


previously set mark.
Byte Stream
 Class hierarchy figure for Writer Class
Methods In Output Stream

 Void close() Closes the Output Stream.

 Void flush() Finalizes the output state so that any


buffers are cleared, it flushes the output buffers.

 Void write(int b) Writes a single byte to an output


Stream.
File Streams
 The File streams

[Link]
This creates an InputStream that we can use to read
bytes from a file. Two constructors are there,
FileInputStream(String filepath)
FileInputStream(File fileobj)

[Link]
This creates an OutputStream that we can use to Write
bytes to a file. Four constructors are there,
FileOutputStream(String filepath)
FileOutputStream(File fileobj)
FileOutputStream(String filepath, boolean append)
FileOutputStream(File fileobj, boolean append)
ByteArrayInputStream

 Is an implementation of an input stream that uses the


byte array as the source.

 There are two constructors


ByteArrayInputStream(byte array[])
ByteArrayInputStream(byte array[], int start, int numBytes)
ByteArrayOutputStream

 Is an implementation of an output stream that uses the


byte array as the destination.

 There are two constructors


ByteArrayOutputStream()
ByteArrayOutputStream( int numBytes)
Pipe Streams

 Pipes are used to channel the output from


one thread into the input of another.

 PipedReader and PipedWriter implement the


input and output components of a pipe
Working with Filter Streams

 The [Link] package provides a set of


abstract classes that define and
partially implement filter streams. A
filter stream filters data as it's being
read from or written to the stream.

 The filter streams are


[Link]
[Link]
Object Serialization

 Two stream classes in [Link],


1. ObjectInputStream
2. ObjectOutputStream

are used to read and write objects.

 The key to writing an object is to represent its state in


a serialized form sufficient to reconstruct the object as
it is read. This process is called object serialization.

You might also like