Java InputStream.read()

Syntax

InputStream.read() has the following syntax.

public abstract int read()   throws IOException

Example

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


/* www . j  av a 2s .c om*/

import java.io.FileInputStream;
import java.io.InputStream;

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

    int i;

    // new input stream created
    InputStream is = new FileInputStream("C://test.txt");

    System.out.println("Characters printed:");

    // reads till the end of the stream
    while ((i = is.read()) != -1) {
      // converts integer to character
      char c = (char) i;

      System.out.print(c);
    }
    is.close();
  }
}