Difference Between InputStream and OutputStream in Java
A stream can be defined as the sequence of data or continuous flow of data. Streams are a clear way to deal with Input/Output. Streams are of two types as Depicted below:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In the above diagram, our InputStream and OutputStream will reside in Byte Stream. So let’s discuss byte Stream.
1. Byte Stream: Byte Stream provides a convenient way of handling the input and output of byte. The byte stream is further divided into various classes but the top hierarchy Classes are depicted below:
1.1 InputStream: InputStream is an abstract class of Byte Stream that describe stream input and it is used for reading and it could be a file, image, audio, video, webpage, etc. it doesn’t matter. Thus, InputStream read data from source one item at a time.
1.2 OutputStream: OutputStream is an abstract class of Byte Stream that describes stream output and it is used for writing data to a file, image, audio, etc. Thus, OutputStream writes data to the destination one at a time.
Difference between InputStream and OutputStream
|1. It is an abstract class that describes Stream Input.||1. It is an abstract class that describes Stream Output.|
|2. InputStream Read data from the source once at a time.||2. OutputStream Write Data to the destination once at a time.|
3. InputStream consist of method which performs:
3. Output Stream consists of methods which perform:
4. Types of InputStream are:
In these types the most important and mostly used type is FileInputStream.
4. Types of OutputStream are:
In these types the most important and mostly used type is FileOutput Stream.
Program for InputStream:
In this Program, the file gfg.txt consist of “GEEKSFORGEEKS”.
Note: In the file is saved in the same location where java Program is saved then follow the below program. If file is saved at some specific location then write the details like.
FileInputStream fileIn=new FileInputStream("C:\\gfg.txt");
Program for OutputStream
Here gfg.txt file is empty and saved in the same location where Java Program is saved. This program writes GeeksforGeeks in the empty file and shows the message “file is successfully updated” if the text is successfully written in the file.
file is successfully updated!!
When we again Read the file using the first program then it is shown below output: