Java IO Tutorial - Java Console.reader()








Syntax

Console.reader() has the following syntax.

public Reader reader()

Example

In the following code shows how to use Console.reader() method.

/*  w w w . ja va2 s . c  om*/
import java.io.Console;
import java.util.Scanner;

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

    Console cnsl = System.console();

    if (cnsl != null) {
      System.out.print("Enter name : ");
      Scanner scan = new Scanner(cnsl.reader());
      while (scan.hasNext()) {
        String str = scan.next();
        System.out.println(str);
      }
    }
  }
}

The code above generates the following result.