While coding your programs, many a times you will feel the need to interact with the user or any data source. This need is accomplished with the help of Java I/O Streams.

What are streams?

Streams in Java refer to the inward and outward flow of data.

An Input Stream is used to read data into the program from any of the input source.

And an Output Stream is used to send data from the program to any of the output sources. Hence it is used for writing data on to the destination.

The data can take the format of numbers, strings, byte, and floating point to name a few. Java contains a package java.io which consists of all the classes which help in the streaming of data.

Two Main Types of Java I/O Streams

Java IO Streams

Byte Streams

As the name suggests, these streams are meant for handling input and output of bytes like for example when handling binary data this stream is used.

Character Streams

Character streams are specialized streams. These streams handle textual or character based data.

The Byte Stream Classes

Byte Streams in Java are used to perform input and output operations on 8 bit data. Byte Streams are defined mainly by two abstract classes namely InputStream and OutputStream.

As these classes are abstract in nature, objects cannot be directly created for these classes. Hence objects for their subclasses can be created for accessing these classes indirectly.

Some of the subclasses are as follows:

FileInputStream Input stream to read a file.
FileOutputStream Output stream to write to a file.
DataInputStream Input stream to read integers and floating point numbers.
DataOutputStream Output stream to write integers and floating point numbers.

NOTE: – InputStream is the superclass for all input streams whereas; OutputStream is the superclass for all output streams.

For further manipulations, many key methods are used. Some of the most important key methods are:

  1. read() : – This method is used to read bytes of data.
  2. write() : – This method is used to write bytes of data.
  3. close() : – This method is used to close the current input or output stream.

The Character Stream Classes

Character Streams are used to perform input and output operations on 16 bit data represented by Unicode or ASCII character set. Character Streams are defined mainly by two abstract classes namely Reader and Writer.

The Reader class defines the character stream input and the writer class defines the character stream output.

Some of its important subclasses are as follows:

BufferedReader It is used to handle buffered input stream.
BufferedWriter It is used to handle buffered output stream.
FileReader It handles the reading of character streams from a file.
FileWriter It handles the writing of character streams to a file.

Standard Streams

Besides the above streams, Java has some predefined streams for its users . Java defines three predefined streams or standard streams. These are as follows:

System.in This is the standard stream for input. This stream is used for reading data for the program.
System.out This is the standard stream for output. This stream is used for writing data from the program to an output device such as a monitor or to some file.
System.err This is a standard stream for error. This is used to show an error message on the screen to the users.