Java BufferedInputStream read from console

Description

Java BufferedInputStream read from console



import java.io.BufferedInputStream;
import java.io.IOException;

public class Main {
  public static String readLine() {
    StringBuilder response = new StringBuilder();
    try {//from   www  .j av a 2  s  . c o m
      BufferedInputStream bin = new BufferedInputStream(System.in);
      int in = 0;
      char inChar;
      do {
        in = bin.read();
        inChar = (char) in;
        if (in != -1) {
          response.append(inChar);
        }
      } while ((in != -1) & (inChar != '\n'));
      bin.close();
      return response.toString();
    } catch (IOException e) {
      System.out.println("Exception: " + e.getMessage());
      return null;
    }
  }

  public static void main(String[] arguments) {
    String input = readLine();
    System.out.println(input);
  }
}



PreviousNext

Related