Java treats flow of data as stream.
Java streams are classified into two basic
types, namely, input stream and output stream. The [Link] package contains a large
number of stream classes to support the streams. flow of information (data) from
source to destination
Java provides [Link] package which contains a large number of stream classes to
process all types of data
Byte stream classes
• Support for handling I/O operations on bytes
Character stream classes
• Supports for handling I/O operations on characters
Byte streams
This Stream handles the 8-bit binary input/output of data. Byte streams are
used where the program needs to work with the raw binary data. In Java to handle I/O
raw binary data the byte stream classes are defined.
Classes of byte stream
InputStream : An abstract super class of all the classes which represents the
input data as byte (8-bit). The InputStream class is used for reading the data such as a
byte and array of bytes from an input source. An input source can be a file, a string,
or memory that may contain the data. An input stream is automatically opened when
you create it. You cans explicitly close a stream with the close( ) method,
Methods of InputStream
available() : returns the number of readable bytes from the input stream.
public int available() throws IOException
close() : This method is used to close the input stream and the resource
associated to the stream is get released.
public void close() throws IOException
mark(int readLimit) : This method is used to mark the current position in the
input stream.
public void mark(int readlimit)
markSupported() : This method is used to specify that whether the input stream
supports the mark and reset methods.
public boolean markSupported()
read() : This method is used to read next byte of data of input stream.
public abstract int read() throws IOException
read(byte[] b) : This method is used to read the byte from the input stream and
kept them to the buffer array.
public int read(byte[] b) throws IOException
read(byte[] b, int off, int len) : This method is used to read bytes from the input
stream upto the len bytes.
public int read(byte[] b, int off, int len) throws IOException
reset() : This method is used to repositioned the stream where the mark method
was marked the current position in the input stream.
public void reset() throws IOException
skip(long n) : This method is used to skipped over and discarded the data of n
bytes from the input stream.
public long skip(long n) throws IOException
Some subclasses of the InputStream class are as follows :
Byte Stream Classes Description
FileInputStream FileInputStream takes input bytes from a file.
A ByteArrayInputStream can retain bytes read from the
ByteArrayInputStream
input stream using its internal buffer
The input streams can be logically concatenated using
SequenceInputStream
SequenceInputStream.
The string's content supplies bytes read to an input stream
which is created by an application.
StringBufferInputStream
The StringBufferInputStream class allows an application to
do this.
It has some additional input stream used as basic data
FilterInputStream source, these input streams can
provide transformation of data and extra functionality.
It allows an application to read java's data type using an
DataInputStream
input stream which is independent of machine.
It provides additional functionality to the input stream such
BufferedInputStream as capability to buffer. It also supports reset and mark
methods.
The data byte written on piped output stream is provided by
PipedInputStream PipedInputStream. But before that it must connect to the
piped output stream.
To sum up the ability to "push back" or "unread" one byte to
PushbackInputStream
other input stream, a PushbackInputStream is used.
OutputStream : An abstract super class of all the classes which represents the output
data as byte (8-bit). The OutputStream class used for writing byte and array of
bytes to an output source, can be anything such as a file, a string, or memory
containing the data. an output stream is automatically opened when you create it. You
can explicitly close an output stream with the close( ) method.
Methods of OutputStream
close() : This method is used to close the output stream and the resource
associated to the stream is get released.
public void close() throws IOException
flush() : This method is used to flush the output stream and if there is any
unwritten output bytes in the buffer forced to write out them.
public void flush() throws IOException
write(byte[] b) : This method is used to write the [Link] bytes of a specified
byte array to the output stream.
public void write(byte[] b) throws IOException
write(byte[] b, int off, int len) : This method is used to write the specified byte
(len) of a specified byte array started from the specified offset (off) to the
output stream.
public void write(byte[] b, int off, int len) throws IOException
write(int b) : This method is used to write the specified byte into the output
stream.
public abstract void write(int b) throws IOException
Some subclasses of OutputStream class are as follows :
Byte Stream Classes Description
FileOutputStream is used write data to a File or to a
FileOutputStream
FileDescriptor.
ByteArrayOutputUsingStream provides the facility to write
ByteArrayOutputStream
data into a byte array using the output stream.
All the classes that filter output streams is the subclasses of
FilterOutputStream
the FilterOutputStream superclass.
It allows an application to write java's data type(primitive) to
DataOutputStream
an output stream.
The BufferedOutputStream provides the facility to contains
BufferedOutputStream
the byte into the buffered output stream.
It provide ability to the other output stream to print several
PrintStream
data value's representation.
For creating communication pipe, piped output stream
PipedOutputStream
should be connected to piped input stream.
Java I/O Character Streams
Character streams works with the characters rather than the byte. In Java
characters are stored by following the Unicode (allows a unique number for every
character) conventions. In such kind of storage characters becomes the platform
independent, program independent, language independent
In Java character stream I/O the character stream classes are defined. Reader
and Writer classes. Both of these classes are abstract classes. where the Reader class is
used for input (read from) and the Writer class is used for output (write to). To work
with the file I/O specialized classes are the FileReader and FileWriter.
Reader : An abstract class that facilitate to take input (read) the character stream.
Subclasses of this class must have to implement the read(char[], int, int) and close()
methods.
Method Description
close() : This method is used to close the resource and the resource associated
to the stream get released.
publicvoid close() throws IOException
mark(int readAheadLimit) : This method is used to mark the current position
in the stream. This method operation is not supported by the all character-input
stream.
public void mark(int readAheadLimit) throws IOException
markSupported() : This method specifies that whether the mark() operation is
supported by the stream or not. Default value is false.
public boolean markSupported()
read() : This method is used to read the characters. It reads a single character.
It returns the number of read characters in the range 0 to 65535 or -1 if it
reaches the end of the stream.
public int read() throws IOException
read(char[] cbuf) : This method is used to read the characters. Characters are
read into an array. This method returns the number of read characters or -1 if it
reaches the end of stream.
public int read(char[] cbuf) throws IOException
read(char[] cbuf, int off, int len) : This method is used to read the characters.
Characters are read into an array at specified len.
public int read(char[] cbuf, int off, int len) throws IOException
reset() : This method repositioned the stream where(if) the mark method was
marked the current position in the input stream. Or reset according to the
particular stream.
public void reset() throws IOException
skip(long n) : This method is used to skipped over the characters in the stream.
public long skip(long n) throws IOException
Some subclasses of the Reader class are given into table:
Classes Description
BufferedReader class provides the facility to read texts from a
BufferedReader
character-input stream using buffer.
CharArrayReader implements the character buffer to read
CharArrayReader
character stream.
FilterReader This class is used for reading the filtered character streams.
InputStreamReader This class acts as a bridge from byte streams to character streams.
PipedReader Piped character input stream
Writer : An abstract class that facilitate to write character streams. Subclasses of this
class must have to implement the write(char[], int, int), flush() and close() methods.
Method Description
append(char c) : This method adds the specified character at the end to the
stream.
append(CharSequence csq, int start, int end) : This method is used to add
the character to the writer stream in the subsequence of the character sequence.
close() : This method is used to close the writer stream.
public void close() throws IOException
flush() : This method is used to flush the writer stream.
public void flush() throws IOException
write(char[] cbuf) : This method is used to write the array of characters.
public void write(char[] cbuf) throws IOException
write(char[] cbuf, int off, int len) : This method is used to write the specified
portion of an Array of characters.
public void write(char[] cbuf, int off, int len) throws IOException
Some subclasses of Writer class are as follows :
Classes Description
BufferedWriter class provides the facility to write texts to a
BufferedWriter
character-output stream using buffer.
CharArrayWriter implements the character buffer to write
CharArrayWriter
character stream.
FilterWriter This class is used for writing the filtered character streams.
This class acts as a bridge from character streams to byte
OutputStreamWriter
streams.
PipedWriter Piped character output stream
This class is used to write the formatted representation of objects
PrintWriter
to a text output stream.