Java - InputStreamReader read() method



Description

The Java InputStreamReader read() method reads characters from an input stream and returns them as integers. It is commonly used for reading text files or character streams. Returns -1 if the end of the stream (EOF) is reached.

Declaration

Following is the declaration for java.io.InputStreamReader.read() method −

public int read()

Parameters

NA

Return Value

The method returns the character read, or -1 if the end of the stream has been reached.

Exception

IOException − If an I/O error occurs

Example - Usage of InputStreamReader read() method

The following example shows the usage of Java InputStreamReader read() method.

InputStreamReaderDemo.java

package com.tutorialspoint;

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

public class InputStreamReaderDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      InputStreamReader isr = null;
      char c;
      int i;
      
      try {
         // new input stream reader is created 
         fis = new FileInputStream("test.txt");
         isr = new InputStreamReader(fis);
         
         // read till the end of the file
         while((i = isr.read())!=-1) {
         
            // int to character
            c = (char)i;
            
            // print char
            System.out.println("Character Read: "+c);
         }
         
      } catch (Exception e) {
         // print error
         e.printStackTrace();
      } finally {
         // closes the stream and releases resources associated
         if(fis!=null)
            fis.close();
         if(isr!=null)
            isr.close();
      }   
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Character Read: A
Character Read: B
Character Read: C
Character Read: D
Character Read: E

Example - Reading One Character at a Time

The following example shows the usage of Java InputStreamReader read() method.

InputStreamReaderDemo.java

package com.tutorialspoint;

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

public class InputStreamReaderDemo {
   public static void main(String[] args) {
      try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
         int data;
         while ((data = reader.read()) != -1) { // Read character by character
            System.out.print((char) data); // Convert integer to character and print
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Hello

Explanation

  • Uses InputStreamReader to read text from "example.txt".

  • Calls read() to read one character at a time.

  • Converts the integer Unicode value into a character ((char) data).

  • Stops reading when read() returns -1 (EOF reached).

Example - Using read() to Store Data Before Printing

The following example shows the usage of Java InputStreamReader read() method.

InputStreamReaderDemo.java

package com.tutorialspoint;

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

public class InputStreamReaderDemo {
   public static void main(String[] args) {
      try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
         StringBuilder content = new StringBuilder();
         int data;

         while ((data = reader.read()) != -1) { // Read one character at a time
            content.append((char) data); // Store in StringBuilder instead of printing immediately
         }

         // Print entire content at once
         System.out.println("File Content:\n" + content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello World!")

Let us compile and run the above program, this will produce the following result−

File Content:
Hello World!

Explanation

Use StringBuilder When

  • You need to process or modify the text before printing

  • The file is large, and printing character-by-character is inefficient.

Don't use this approach when

  • You need to print immediately (e.g., for real-time logs or a live feed).

  • The file is too large to fit in memory (use buffered reading instead).

java_io_inputstreamreader.htm
Advertisements