Java BufferedReader console read character input

Introduction

Java console input is done by reading from System.in.

To get a character-based stream attached to the console, wrap System.in in a BufferedReader object.

The following program reads characters from the console until the user types a "q."


// Use a BufferedReader to read characters from the console. 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
  public static void main(String args[]) throws IOException {
    char c;// ww  w . j  a  v a 2 s  . c  o m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter characters, 'q' to quit.");
    // read characters
    do {
      c = (char) br.read();
      System.out.println(c);
    } while (c != 'q');
  }
}



PreviousNext

Related