InputStreamReader in Java (with Example)

An InputStreamReader in Java is a character input stream that uses the stream of bytes as its data source. It acts as a bridge between an incoming stream of bytes and an outgoing sequence of characters and converts a byte stream into a character stream.

Java InputStreamReader reads bytes from a specified InputStream and converts (translates) into Unicode characters according to the default or specified character encoding.

In other words, data read from the source input stream are decoded from bytes using the specified charset.

Java InputStreamReader Class Declaration


An InputStreamReader is a concrete subclass of Reader class that extends Object class. It is also a superclass of FileReader class. It implements Closeable, AutoCloseable, and Readable interfaces.

The general syntax to declare InputStreamReader class in Java is as follows:

public class InputStreamReader
   extends Reader
       implements Closeable, AutoCloseable, Readable

Constructors of InputStreamReader Class


InputStreamReader class defines the following constructors in Java. They are as follows:

1. InputStreamReader​(InputStream in): This constructor creates an InputStreamReader object that uses the default character encoding to convert bytes into characters.

The general syntax to create an object of InputStream class is given below:

InputStreamReader inStream = new InputStreamReader(InputStream in);

Here, the parameter to the constructor of InputStreamReader is of type InputStream, so we can pass an object of any class derived from InputStream to it.
[blocksy-content-block id=”12371″]
For example:

InputStreamReader inStream = new InputStreamReader(System.in);

The above statement creates an InputStreamReader object inStream from the object System.in.

To read keys from the keyboard, we generally use create InputStreamReader stream from System.in and then use read() method of InputStreamReader class to read what we have typed.

2. InputStreamReader​(InputStream in, String charsetName): This constructor creates an InputStreamReader object that uses the named character encoding.

charsetName specifies the character encoding that is used to convert bytes into characters. This constructor throws an exception named UnsupportedEncodingException when named character encoding is not supported.

3. InputStreamReader​(InputStream in, Charset cs): This character creates an InputStreamReader object that uses the specified charset to decode bytes into characters.

4. InputStreamReader​(InputStream in, CharsetDecoder dec): This constructor creates an InputStreamReader object that uses the specified charset decoder.

Methods of InputStreamReader Class in Java


In addition to methods inherited from the Reader class, InputStreamReader class in Java also defines some useful methods. They are as follows:
[blocksy-content-block id=”12121″]
1. String getEncoding(): This method returns the name of the character encoding being used by this stream.

2. int read(): This method reads a single character.

3. int read​(char[ ] charBuffer, int offset, int length): This method reads characters into a portion of an array.

4. boolean ready(): This method checks whether this stream is ready to be read. It returns true if the stream is ready to be read.

InputStreamReader Example Programs


Let’s take a simple example program where we will read data from a file and display it on the console using the input stream reader class.
[blocksy-content-block id=”12153″]
Example 1:

package javaProgram;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args)
{
try {  
// Create an object of FileInputStream class and pass path of filename.
   FileInputStream fis = new FileInputStream("D:\\myfile.txt");	
	
// Create an object of InputStreamReader class and pass reference variable fis to its constructor. 
   InputStreamReader inStream = new InputStreamReader(fis);  

   int data = inStream.read(); // Calling to read() method. 
   while (data != -1) {  
     System.out.print((char) data);  
     data = inStream.read();  
   } 
   inStream.close();  
} catch (Exception ex) {  
     System.out.println(ex.getMessage());  
  }  
 }
}

myfile.txt file contents:

Welcome to Java Programming.
Output:
            Welcome to Java Programming.

Example 2: Implementation of ready() and getEncoding() method.

package javaProgram;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args)
{
try {  
// Create an object of FileInputStream class and pass path of filename.
   FileInputStream fis = new FileInputStream("D:\\myfile.txt");	
	
// Create an object of InputStreamReader class and pass reference variable fis to its constructor. 
   InputStreamReader inStream = new InputStreamReader(fis);  

// Calling getEncoding() method to get the character encoding present in the stream.
   String encoding = inStream.getEncoding();
   System.out.println("Name of encoding used : "+encoding);
  
   int byte1 = inStream.read(); // Calling to read() method to read data.
   char ch1 = (char)byte1;
   System.out.println(ch1);

// Checking the stream inStream ready to be read.
   boolean b = inStream.ready(); // Use of ready() method.
   System.out.println("Ready? : "+b);
 
   int byte2 = inStream.read();
   char ch2 = (char)byte2;
   System.out.println(ch2);
   System.out.println("Ready? : " +inStream.ready());
  
   int byte3 = inStream.read();
   char ch3 = (char)byte3;
   System.out.println(ch3);
   System.out.println("Ready? : " +inStream.ready());
  
   int byte4 = inStream.read();
   char ch4 = (char)byte4;
   System.out.println(ch4);
   System.out.println("Ready? : " +inStream.ready());
 
  inStream.close();  
  fis.close();
} catch (Exception ex) {  
     System.out.println(ex.getMessage());  
  }  
 }
}

myfile.txt file contents:

Java
Output:
           Name of encoding used : Cp1252
           J
           Ready? : true
           a
           Ready? : true
           v
           Ready? : true
           a
           Ready? : false

Let’s create a program where we will take a character as input from the keyboard and display it on the console. Look at the following source code step by step.

Example 3:

package javaProgram;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args) throws IOException
{
// Create an InputStreamReader object using standard input stream.
   InputStreamReader isr = new InputStreamReader(System.in);
   
   System.out.println("Enter a character:");
   char ch = (char)isr.read();
   System.out.println("Input Character: " +ch);
 }
}
Output:
            Enter a character:
            D
            Input Character: D

Hope that this tutorial has covered almost all important points related to InputStreamReader class in Java with example programs. I hope that you will have understood the basic concept of InputStreamReader class. If you find anything incorrect in this tutorial, please inform us via email. Your email will be valuable.
Thanks for reading!!!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.