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
// Imported to use methods
import java.io.FileInputStream;
// Main Class
public class InputStreamExample {
public static void main(String args[])
{
// Reading from Source file
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
// Imported to use inbuilt methods
import java.io.FileOutputStream;
// Main class
public class OutputStreamExample {
public static void main(String args[])
{
// Writing in file gfg.txt
try {
FileOutputStream fileOut
= new FileOutputStream("gfg.txt");
String s = "GeeksforGeeks";
// converting string into byte array
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
Similar Reads
Implement how to load File as InputStream in Java Problem Statement: There is a file already existing in which the same file needs to be loaded using the InputStream method. Concept: While accessing a file either the file is being read or write. here are two streams namely fileInputStream and fileOutputStream Java Stream is the flow of data from
4 min read
How to Convert InputStream to Byte Array in Java? In Java, input stream refers to an ordered flow of data in the form of bytes. This flow of data can be coming from various resources such as files, network programs, input devices, etc. In order to read such data, we have a Java InputStream Class in the Java IO API. There are several methods to conv
5 min read
Different Ways to Copy Content From One File to Another File in Java In Java, we can copy the contents of one file to another file. This can be done by the FileInputStream and FileOutputStream classes. FileInputStream Class It is a byte input stream class which helps in reading the bytes from a file. It provides different methods to read the data from a file. FileInp
3 min read
Java Program to Convert InputStream to String Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read
4 min read
Java Program to Convert String to InputStream Given a string, the task is to convert the string to InputStream which is shown in the below illustrations. Illustration: Input : String : "Geeks for Geeks" Output : Input Stream : Geeks for Geeks Input : String : "A computer science portal" Output : Input stream : A computer science portal In order
2 min read
Java Program to Write Bytes using ByteStream Java Byte streams are used to perform input and output of 8-bit bytes. To write Bytes using BytesStream to a file Java provides a specialized stream for writing files in the file system known as FileOutputStream. This stream provides the basic OutputStream functionality applied for writing the conte
4 min read
Java Program to Convert OutputStream to String OutputStream is an abstract class that is available in the java.io package. As it is an abstract class in order to use its functionality we can use its subclasses. Some subclasses are FileOutputStream, ByteArrayOutputStream, ObjectOutputStream etc. And a String is nothing but a sequence of character
2 min read
Java Program to Create a New File There are two standard methods to create a new file, either directly with the help of the File class or indirectly with the help of the FileOutputStream class by creating an object of the file in both approaches.Methods to Create Files in JavaThere are two methods mentioned belowUsing the File Class
4 min read
Functional Programming in Java 8+ using the Stream API with Example API is an acronym for Application Programming Interface, which is software and the java streams work on a data source. Consider a stream like a flow of water in a small canal. Let's take a real-life example. Each time a user uses an application that is popular these days like WhatsApp in order to co
4 min read
Java Program to Write into a File FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:Using writeString(
5 min read