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
concat() Method of Stream Interface in Java API Stream Interface in Java API is present in Java.Util.Stream and is extending the BaseStream<T, Stream<T>> interface. This interface is created on any stream of objects and it provides a lot of methods that can be used to modify the stream of those objects. concat() Method: The name of th
3 min read
LongStream.Builder build() in Java LongStream.Builder build() builds the stream, transitioning this builder to the built state. Syntax : LongStream build() Return Value: This method returns the built stream. Note: A stream builder has a lifecycle, which starts in a building phase, during which elements can be added, and then transiti
2 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
How to Read and Write Binary Files in Java? The Binary files contain data in a format that is not human-readable. To make them suitable for storing complex data structures efficiently, in Java, we can read from and write to binary files using the Input and Output Streams.In this article, we will learn and see the code implementation to read a
2 min read
Program to convert Boxed Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case
3 min read