Contents
Types of Streams in Java
What is a Stream?
A stream is an abstract representation of a sequence of objects from a source. The source of the stream can be a collection, an array, a generator function, or an I/O channel. A stream does not store the element but instead provides access to them one at a time. As soon as the elements are processed, they are discarded from the source.
Types of Streams in Java
Java provides four types of streams to work with:
- Byte Streams – handle input and output of bytes.
- Character Streams – handle input and output of characters.
- Object Streams – handle input and output of objects.
- Data Streams – handle input and output of primitive data types.
Byte Streams
Byte streams provide a convenient means for handling input and output of bytes. Examples of byte stream classes are FileInputStream and FileOutputStream. These classes are used for reading and writing raw bytes from a file.
Character Streams
Character streams provide a convenient means for handling input and output of characters. Examples of character stream classes are FileReader and FileWriter. These classes are used for reading and writing characters from a file.
Object Streams
Object streams provide a convenient means for handling input and output of objects. Examples of object stream classes are ObjectInputStream and ObjectOutputStream. These classes are used for reading and writing objects from a file.
Data Streams
Data streams provide a convenient means for handling input and output of primitive data types. Examples of data stream classes are DataInputStream and DataOutputStream. These classes are used for reading and writing primitive data types from a file.
In conclusion, Java provides four types of streams for handling input and output of data: byte streams, character streams, object streams, and data streams. Each of these streams provides a convenient means for handling input and output of data.