Java InputStreamReader Class

The InputStreamReader class of the java.io package can be used to convert data in bytes into data in characters.

It extends the abstract class Reader.

The InputStreamReader class is a suclass of Java Reader.

The InputStreamReader class works with other input streams. It is also known as a bridge between byte streams and character streams. This is because the InputStreamReader reads bytes from the input stream as characters.

For example, some characters required 2 bytes to be stored in the storage. To read such data we can use the input stream reader that reads the 2 bytes together and converts into the corresponding character.


Create an InputStreamReader

In order to create an InputStreamReader, we must import the java.io.InputStreamReader package first. Once we import the package here is how we can create the input stream reader.

// Creates an InputStream
FileInputStream file = new FileInputStream(String path);

// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);

In the above example, we have created an InputStreamReader named input along with the FileInputStream named file.

Here, the data in the file are stored using some default character encoding.

However, we can specify the type of character encoding (UTF8 or UTF16) in the file as well.

// Creates an InputStreamReader specifying the character encoding
InputStreamReader input = new InputStreamReader(file, Charset cs);

Here, we have used the Charset class to specify the character encoding in the file.


Methods of InputStreamReader

The InputStreamReader class provides implementations for different methods present in the Reader class.

read() Method

  • read() - reads a single character from the reader
  • read(char[] array) - reads the characters from the reader and stores in the specified array
  • read(char[] array, int start, int length) - reads the number of characters equal to length from the reader and stores in the specified array starting from the start

For example, suppose we have a file named input.txt with the following content.

This is a line of text inside the file.

Let's try to read this file using InputStreamReader.

import java.io.InputStreamReader;
import java.io.FileInputStream;

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

    // Creates an array of character
    char[] array = new char[100];

    try {
      // Creates a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");

      // Creates an InputStreamReader
      InputStreamReader input = new InputStreamReader(file);

      // Reads characters from the file
      input.read(array);
      System.out.println("Data in the stream:");
      System.out.println(array);

      // Closes the reader
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Data in the stream:
This is a line of text inside the file.

In the above example, we have created an input stream reader using the file input stream. The input stream reader is linked with the file input.txt.

 FileInputStream file = new FileInputStream("input.txt");
 InputStreamReader input = new InputStreamReader(file);

To read characters from the file, we have used the read() method.


getEncoding() Method

The getEncoding() method can be used to get the type of encoding that is used to store data in the input stream. For example,

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;

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

    try {
      // Creates a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");

      // Creates an InputStreamReader with default encoding
      InputStreamReader input1 = new InputStreamReader(file);

      // Creates an InputStreamReader specifying the encoding
      InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));

      // Returns the character encoding of the input stream
      System.out.println("Character encoding of input1: " + input1.getEncoding());
      System.out.println("Character encoding of input2: " + input2.getEncoding());

      // Closes the reader
      input1.close();
      input2.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

The character encoding of input1: Cp1252
The character encoding of input2: UTF8

In the above example, we have created 2 input stream reader named input1 and input2.

  • input1 does not specify the character encoding. Hence the getEncoding() method returns the canonical name of the default character encoding.
  • input2 specifies the character encoding, UTF8. Hence the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding. To learn more, visit Java Charset (official Java documentation).


close() Method

To close the input stream reader, we can use the close() method. Once the close() method is called, we cannot use the reader to read the data.


Other Methods of InputStreamReader

Method Description
ready() checks if the stream is ready to be read
mark() mark the position in stream up to which data has been read
reset() returns the control to the point in the stream where the mark was set

To learn more, visit Java InputStreamReader (official Java documentation).

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges