InputStreamReader in Java | Methods, Example – Scientech Easy

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.

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.

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:

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

1. 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.

Program code 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.

Program code 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

3. 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.

Program code 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.

In the next tutorial, we will discuss another important topic FileReader class in Java with examples. If you find anything incorrect in this tutorial, please inform us via email. Your email will be valuable.
Thanks for reading!!!
Next ⇒ FileReader in Java

⇐ Prev Next ⇒