Difference Between InputStream and OutputStream in Java
Last Updated :
28 Jan, 2021
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:
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
InputStream |
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:
- Read next byte of data from the input stream and return -1 at the end of the file: public abstract int read()throws IOException
- Close current InputStream: public int available()throws IOException
- Returns an estimate of the number of bytes that can be read from the current input stream: public void close()throws IOException
|
3. Output Stream consists of methods which perform:
- Write a byte to current Outputstream : public void write(int)throws IOException
- Write array of byte to current output stream : public void write(byte[])throws IOException
- Flushes the current OutputStream: public void flush()throws IOException
- Close current Output Stream. : public void close()throws IOException
|
4. Types of InputStream are:
- FileInputStream
- ByteArrayInputStream
- FilterInputStream
- ObjectInputStream
In these types the most important and mostly used type is FileInputStream.
|
4. Types of OutputStream are:
- FileOutputStream
- ByteArrayOutputStream
- FilterOutputStream
- ObjectOutputStream
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");
Java
import java.io.FileInputStream;
public class InputStreamExample {
public static void main(String args[])
{
try {
FileInputStream fileIn
= new FileInputStream( "gfg.txt" );
int i = 0 ;
while ((i = fileIn.read()) != - 1 ) {
System.out.print(( char )i);
}
fileIn.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
GEEKSFORGEEKS
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.
Java
import java.io.FileOutputStream;
public class OutputStreamExample {
public static void main(String args[])
{
try {
FileOutputStream fileOut
= new FileOutputStream( "gfg.txt" );
String s = "GeeksforGeeks" ;
byte b[] = s.getBytes();
fileOut.write(b);
fileOut.close();
System.out.println(
"file is successfully updated!!" );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
file is successfully updated!!
When we again Read the file using the first program then it is shown below output:
GeeksforGeeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...