Difference Between InputStream and OutputStream in Java - GeeksforGeeks

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.
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.
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 methodsimport java.io.FileInputStream;  // Main Classpublic 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 
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 methodsimport java.io.FileOutputStream;  // Main classpublic 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
java-stream
Picked
Technical Scripter 2020
Difference Between
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Differences and Applications of List, Tuple, Set and Dictionary in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java