1/12/12

A Stream is a flow of data from the source to a sink. Source and sink are also called input and output streams, respectively.The I/O Streams are of two Kinds :
1. Byte Streams 2. Character Streams
Normally the term stream refers to the byte stream and the terms reader and writer refer to the character stream.

Byte Streams

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.

The InputStream Methods

The first method returns an int, which contains either a byte read from the stream, or a -1, which indicates the end of file condition. The other two methods read the stream into a byte array and return the number of bytes read.

When you have finished with a stream, close it. If you have a stack of streams, use filter streams to close the stream at the top of the stack. This operation also closes the lower streams.int available ()This method reports the number of bytes that are immediately to be read from the stream.

The OutputStream Methods

When you have finished with a stream, close it. If you have a stack of streams, use filter streams to close the stream at the top of the stack. This operation also closes the lower streams.

void flush ()

Sometimes the stream accumulates writes before committing them. The flush() method enables us to force the writes.

Character Stream

The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.
All character stream classes are descended from Reader and Writer. As with byte streams, there are character stream classes that specialize in file I/O.

The Reader Method

int read()int read(char[] cbuf)int read(char[] cbuf, int offset, int length)The first method returns an int, which contains either a byte read from the stream, or a -1, which indicates the end of file condition. The other two methods read the stream into a character array and return the number of bytes read.

Handling the buffer is tedious and error-prone, but Java has some predefined classes like BufferedReader which help us to read using the buffer and thus take care of all the errors and tediousness.

Buffered Stream : The below program also does what the above program does, but the difference is that this uses buffered streams instead of character streams. The advantage is that Buffered Stream read one line at a time unlike Character Stream which read one character at a time.

3 comments:

Though Streams are good and provide a consistent way of reading and writing data.Biggest disadvantage of using Streams is that they are blocking call and can hinder performance, there synchrony alternatives java.nio and channel are much better for high performance application. I have also shared my view on Using memory mapped file for high performance IO, you may find useful.