Example of the read() method

import java.io.*;


public class Echo {

  public static void main(String[] args) {
  
    echo(System.in);
  
  }
  
  public static void echo(InputStream in) {
  
    try {
      while (true) {
        // Notice that although a byte is read, an int
        // with value between 0 and 255 is returned.
        // Then this is converted to an ISO Latin-1 char 
        // in the same range before being printed.   
        int i = in.read();
        // -1 is returned to indicate the end of stream
        if (i == -1) break;
        
        // without the cast a numeric string like "65"
        // would be printed instead of the character "A"
        char c = (char) i; 
        System.out.print(c);    
      }
    }
    catch (IOException e) {
      System.err.println(e); 
    }
    System.out.println();  
  
  }

}

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 28, 2000