Site Search:

Read and write data from the console

Back>

System.console()
System.console()

Start from Java 6, the java.io.Console class is introduced to interact with user. Console is a singleton, it is created automatically by the JVM and accessed by calling the System.console() method.  When text interactions are not supported, call System.console() returns null.

Console's reader() returns an instance of Reader, and writer() returns an instance of PrintWriter. Once get PrintWriter instance, you can call methods such as format, println.

Console provided the following two convenient methods for user input.

  • String input = System.Console().readLine("prompt msg for User");
  • char[] passed = System.Console().readPassword("prompt msg for User");
The reason readPassword() returns a char array instead of String is to avoid put sensitive information into string pool.

OCPJP>cat ConsoleTest.java 
import java.io.*;
import java.util.*;
public class ConsoleTest {
    public static void main(String[] args) {
        Console console = System.console();
        if(console == null) {
            System.out.println("Console not supported, use System.in and System.out instead.");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                System.out.flush();
                String in = br.readLine();
                System.out.println("your input:" + in);
                System.out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            PrintWriter writer = console.writer();
            Reader r = console.reader();
            BufferedReader reader = new BufferedReader(r);
            
            String msg = "hello, flush() before readLine() and readPassword()";
            writer.println(msg);
            writer.format(Locale.CANADA_FRENCH, msg);
            writer.println();
            writer.flush();
            try {
                String input = reader.readLine();
                writer.write("read user input from BufferedReader: " + input);
                writer.println();
                writer.flush();
                input = System.console().readLine("please input something:");
                System.out.println("you have input: " + input);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            writer.flush();
            char[] password = System.console().readPassword("now your password:");
            for(char i : password) {
                writer.append(i);
            }
            writer.println();
            writer.flush();
            //clear password from memory
            for(int i = 0; i < password.length; i++ ) {
                password[i] = 'x';
            }
        }
    }
}
OCPJP>
OCPJP>
OCPJP>javac ConsoleTest.java 
OCPJP>java ConsoleTest
hello, flush() before readLine() and readPassword()
hello, flush() before readLine() and readPassword()
ok
read user input from BufferedReader: ok
please input something:something
you have input: something
now your password:
12345
OCPJP>


Last but not least, you should fall back to the old way of System.in and System.out if the System.console() returns null.