Java InputStreamReader Class

Java InputStream Class

InputStreamReader Class

In the Article, we will Discuss about the InputStreamReader class of java.
In Java, the InputStreamReader class is used to read characters from an input stream, which can be a file, network socket, or any other data source that contains character data.

Java InputStreamReader Class:

The Java InputStreamReader Class is a subclass of the abstract class Reader and provides a bridge between byte streams and character streams. The class reads bytes from the input stream and decodes them into characters using a specified charset.
The InputStreamReader class provides several constructors, which allow you to specify the input stream and the charset to use for decoding bytes into characters. Some of the commonly used constructors are:

Create an InputStreamReader object:

Syntax

// Creating an InputStream
InputStream input = new FileInputStream("file.txt");

// Creating an InputStreamReader
InputStreamReader reader = new InputStreamReader(input);

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

Creating an InputStreamReader with specified encoding Character:

Syntax

// Creating FileInputStream
InputStream input = new FileInputStream("file.txt");

// Initializing character encoding
Charset charset = StandardCharsets.UTF_8;

// Creating InputStreamReader with character 
InputStreamReader reader = new InputStreamReader(input, charset);

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

Example Program for InputStreamReader class:

Run
import java.io.*;

public class Main{
    public static void main(String[] args) {
        // Creating FileInputStream object
        try (FileInputStream input = new FileInputStream("example.txt");
             
             // Creating InputStreamReader object
             InputStreamReader reader = new InputStreamReader(input, "UTF-8")) {

            // Read function for reading the file
            int data = reader.read();
            
            while (data != -1) {
                char character = (char) data;
                System.out.print(character);
                data = reader.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Explanation:

In the above example, we create a FileInputStream object to read bytes from a text file named “example.txt”. We then create an InputStreamReader object that wraps the FileInputStream and uses the UTF-8 character set to decode the bytes into characters.

We use the read() method of the InputStreamReader to read one character at a time from the file, and we print each character to the console. We keep reading characters until the end of the file is reached, which is indicated by the read() method returning -1.

If an IOException occurs during the reading process, we catch it and print the stack trace for debugging purposes.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription