English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collections

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java InputStreamReader class

In this tutorial, we will learn about Java InputStreamReader and its methods with the help of examples.

The InputStreamReader class in the java.io package can be used to convert byte data to character data.

It inherits from the abstract class 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 InputStreamReader reads bytes as characters from the input stream.

For example, some characters require2bytes to store in memory. To read such data, we can use an input stream reader that reads together2bytes and converts them to the corresponding characters.

Create an InputStreamReader

To create an InputStreamReader, we must first import the java.io.InputStreamReader package. After importing the package, we can create an input stream reader.

//Create an InputStream
FileInputStream file = new FileInputStream(String path);
//Create an InputStreamReader
InputStreamReader input = new InputStreamReader(file);

In the above example, we created an InputStreamReader named input and a FileInputStream named file.

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

However, we can also specify the type of character encoding in the file (UTF8OrUTF16)

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

Here, we use the Charset class to specify the character encoding of the file.

Methods of InputStreamReader

The InputStreamReader class provides implementations of different methods that exist in the Reader class.

The read() method

  • read() - Reads a single character from the reader.

  • read(char[] array) - Reads characters from the reader and stores them in the specified array.

  • read(char[] array, int start, int length) - Reads characters from the reader and stores them in the specified array starting from index start.

For example, suppose we have a file namedinput.txtfile that contains the following content.

This is a line of text inside the file.

Let's try to use the InputStreamReader to read this file.

import java.io.InputStreamReader;
import java.io.FileInputStream;
class Main {
  public static void main(String[] args) {
    //Create a character array
    char[] array = new char[100];
    try {
      //Create a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");
      //Create an InputStreamReader
      InputStreamReader input = new InputStreamReader(file);
      //Read characters from the file
      input.read(array);
      System.out.println("Data in the stream:");
      System.out.println(array);
      //Close reader
      input.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

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

In the above example, we created an input stream reader using a file input stream. The input stream reader is linked to the fileinput.txtlink.

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

To read characters from a file, we use the read() method.

The getEncoding() method

The getEncoding() method can be used to obtain the encoding type 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 {
      //Create a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");
      //Create an InputStreamReader using the default encoding
      InputStreamReader input1 = new InputStreamReader(file);
      //Create an InputStreamReader with specified encoding
      InputStreamReader input2 InputStreamReader input = new InputStreamReader(file, Charset.forName("UTF8");
      //Returns the character encoding of the input stream
      System.out.println("input1The character encoding: " + input1.getEncoding());
      System.out.println("input2The character encoding: " + input2.getEncoding());
      //Close reader
      input1.close();
      input2.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

input1Character Encoding: Cp1252
input2Character Encoding: UTF8

In the above example, we created two input stream readers named input1and input2.

  • input1No character encoding is specified. Therefore, the getEncoding() method returns the standard name of the default character encoding.

  • input2Specify Character EncodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding.

close() Method

To close the input stream reader, we can use the close() method. After calling this close() method, we will not be able to use the reader to read data anymore.

Other Methods of InputStreamReader

MethodDescription
ready()Check if the Stream is Ready to Be Read
mark()Mark the Position of the Read Data in the Stream
reset()Reset Markers