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:
|
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");
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 Lazyroar 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 = "Lazyroar" ; // 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:
Lazyroar