Stream in Java represents sequential flow (or unbroken flow) of data from one place to another place.
In other simple words, a stream is a path along which data flows (like a pipe along which water flows).
It is required to accept data as input from the keyboard. The data in the form of stream may be bytes, characters, objects, etc.
Let’s understand it with the help of a realtime example.
A river is a stream of water that flows from one place (i.e. source) to another place (i.e. destination) in a continuous sequence. Source and destination are connected through the continuous flow of water.
Similarly, a stream carries data from one place (source) to another place (destination). In Java I/O, data flows from a source known as data source to a destination known as data sink as shown in the below figure. Data source and data sink are connected through a Java program.
Types of Stream in Java
Basically, there are two types of streams in Java. They are as follows:
- Input stream
- Output stream
Input Stream: A stream that receives or reads data from a data source and sends it to a Java program is called input stream. Input represents the flow of data into a program.
The input stream connects data source and a Java program as shown in the below figure.
As you can see in the above figure, the data is read from a data source and flow sequentially to a Java program.
Output Stream: A stream that takes data from a Java program and sends or writes data to the destination (data sink) is called output stream. A output represents the flow of data out of a program.
Output stream connects java program and a data sink. A Java program writes or sends data to the data sink.
The data flow from a data source to a Java program via an input stream. The data flow from a Java program to a data sink via an output stream.
In simple words, Java program reads data from an input stream and writes data to the output stream. In both input and output streams, Java program does not know the details of source and destination.
How to read data from Data source into Java Program?
To read data from data source into a Java program, we need to perform the following steps that are as follows:
1. Identify the data source. It may be a file, array, string, network connection, etc.
2. Create an input stream by using the data source identified.
3. Read data from the input stream. Basically, we read data in a loop as long as we have received all data from input stream.
4. Now, close the input stream when reading data has finished. From Java 7 onwards, we can use the try-with-resources block that closes the input stream automatically.
How to write Data to Destination from Java Program?
To write data to a destination (data sink) from the Java program, perform the following steps:
1. Identify the destination where data will be written. A destination may be a file, array, string, network connection, etc.
2. Create an output stream using data sink that we have identified.
3. Now, write data to the output stream.
4. Close the output stream once writing data is completed. From Java 7 onwards, we can use a try-with-resources block to close the output stream automatically.
We will perform all these steps with example programs in further tutorials one by one.
Hope that this tutorial has covered almost all the important points concerning stream in Java and its types. I hope that you will have understood the basics points of input stream and output stream.
In the next tutorial, we will learn stream classes in Java such as Byte stream classes and Character stream classes.
Thanks for reading!!!
Next ⇒ Stream Classes in Java