dsa

Java I/O Streams

In this tutorial, we will learn about Java input/output streams and their types.

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

A stream can be defined as a sequence of data. There are two kinds of Streams −

    A stream can be defined as a sequence of data. There are two kinds of Streams −
  1. InPutStream − The InputStream is used to read data from a source.
  2. OutPutStream − The OutputStream is used for writing data to a destination.

Example:

// Java code to illustrate print() 
import java.io.*; 

class Demo_print { 
	public static void main(String[] args) 
	{ 

		// using print() 
		// all are printed in the 
		// same line 
		System.out.print("Codemistic ");
        System.out.print("Codemistic "); 
		System.out.print("Codemistic
		 "); 
	} 
} 
Output:
Codemistic Codemistic Codemistic

Types of Streams

ByteStream

This is used to process data byte by byte (8 bits). Though it has many classes, the FileInputStream and the FileOutputStream are the most popular ones. The FileInputStream is used to read from the source and FileOutputStream is used to write to the destination.

CharacterStream

In Java, characters are stored using Unicode conventions (Refer this for details). Character stream automatically allows us to read/write data character by character. Though it has many classes, the FileReader and the FileWriter are the most popular ones. FileReader and FileWriter are character streams used to read from the source and write to the destination respectively.