Java IO Tutorial - Java InputStreamReader.read()








Syntax

InputStreamReader.read() has the following syntax.

public int read()  throws IOException

Example

In the following code shows how to use InputStreamReader.read() method.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*  w ww  .j  av  a 2s  .c o  m*/
public class Main {
  public static void main(String[] args) throws IOException {
    int i;
    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // read till the end of the file
    while ((i = isr.read()) != -1) {
      // int to character
      char c = (char) i;
      System.out.println("Character Read: " + c);
    }
    isr.close();
  }
}