A stream represents a flow of data, or a channel of communication with a writer at one end and a reader at the other. To handle input and output we use java.io package. Let us discuss the classification of I/O streams.
Classification of I/O streams
- Byte Streams
- Character Streams
- Buffered Streams
- Terminal I/O or Console I/O
- Data Streams
- Object Streams
1. Byte Streams:
The byte streams are used to perform input and output of 8 – bit bytes. All byte stream classes are derived from InputStream and OutputStream.
InputStream and OutputStream are abstract classes that define the lowest-level interface for all byte streams. They contain methods for reading or writing an unstructured sequence of bytes.
Java implements subclasses of these for activities such as reading from and writing to files. Because all byte streams inherit the structure of InputStream or OutputStream, the various kinds of byte streams can be used interchangeably.
All the methods of this class will throw an IO Exception. Some of the byte streams are
2. Character Streams:
The Character Streams are used for reading or writing a sequence of character data, with support for Unicode. All other character streams in Java are built on top of Reader and Writer. Reader and Writer are very much like InputStream and OutputStream, except that they deal with characters instead of bytes. As true character streams, these classes correctly handle Unicode characters. InputStreamReader and OutputStreamWriter are special classes that use a character-encoding scheme to translate between character and byte streams.
All the methods of this class will throw an IO Exception. Some of the byte streams are
OutputStreamWriter
3. Buffered Streams:
A buffer can increase efficiency by reducing the number of physical read or write operations that correspond to read( ) or write( ) method calls.
Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
Some of the buffered streams are
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
BufferedOutputStream
BufferedReader
BufferedWriter
4. Terminal I/O or Console I/O
The terminal or console refers to command line environment or command prompt. The most commonly used standard output is System.out and System.err is derived from OutputStream object and standard input is System.in is derived from InputStream object. The following example shows the correspondence.