Java – Console
To get sensitive user input without echoing it to the console/terminal, the Java SDK has introduced the java.io.Console class. The Console class is quite unique in the way it handles its data. It uses the native encoding of the system instead of the using the JVM’s default encoding.
The Console class provides methods to access the console/terminal, if any is associated with the JVM.
Using the Console class:
So how do you use the Console class ? Its pretty simple really. First get a reference to the Console class from the System class
Console con = System.console();
It is not always possible to get a reference to the console. Some scenarios prevent the developer from gaining access to the console. This method may return null in these cases. For example a batch process running on a server will not expect user input and usually does have a console. Your code must be capable of handling such situations.
Consider that you have a program that requires the user to secretly type in his/her password.
Entering sensitive data through the console:
Console con = System.console(); String user = con.readLine("Username: "); char password = con.readPassword("Password: ");
If you were to run this code using the eclipse IDE it would not work (At least in eclipse 3.2).
This is because eclipse does not handle this code properly as yet. Run this code from the command prompt for now. Here is the output of the program
java com.Tests.Cert Username: hello Password:
Even though the output does not show a password being typed in, the user has typed it in on the console. The console’s feature allows the user to type in the password without it being echoed to the screen.
The line of code con.readLine(“Username: “); reads data from the console normally and stores it into a string. It also echoes whatever the user types onto the screen (you can see “hello” on the output). The line of code con.readPassword(“Password: “); prompts the user for a password but does not echo the contents of the password to the screen. This allows the user to enter his/her password safely using the Console class. To format the output to the screen the following code can be used.
Console con = System.console(); String user = con.readLine("Welcome %s. Please login \nUsername:","Guest"); char password = con.readPassword("What is your password %s:",user); System.out.println("User: " + user ); System.out.println("Password: " + new String (password) );
java com.Tests.Cert Welcome Guest. Please login Username: hello What is your password hello: User: hello Password: bla
Notice that although the password is not visible in the console screen, the program was able to retrieve it. Also the line of code con.readPassword(“What is your password %s: “,user); will format the output such that the username is present when asking for the password. %s represents the String that was passed to the method. Now let’s take a look at using the format() method.
Formatting output only:
Console con = System.console(); con.format("Welcome %s \n", "Mr bond"); String user = con.readLine("Username: "); char password = con.readPassword("What is your password %s:",user); System.out.println("User: " + user ); System.out.println("Password: " + new String (password) );
Welcome Mr bond Username: 007 What is your password 007: User: 007 Password: goldeneye
In this case the formatting that was present in the readLine() method has been transferred to the format() method. Console also has a printf() method that works similar to the format() method. This method however has been made available only for convenience. Finally you can make use of the reader and writer classes to read and write to the Console.
Reader and Writer:
Note: This code throws IOException since we need read/write access to the console
char buffer= new char; Console con = System.console(); Reader reader = con.reader(); int numRead = reader.read(buffer); // #1 System.out.println(new String (buffer).substring(0, numRead) ); Writer writer = con.writer(); // #2 writer.write(buffer); writer.flush(); writer.close();
reader -- #a. Input provided by a user reader -- #b. Written by System.out reader -- #c. Written by console writer
The #a reader is the input that was provided to the program. The program reads this data using the Reader class. It reads the data into a character buffer in line #1 and prints the data to the console . At line #2 the console’s writer is obtained by the program and the contents of the character buffer are sent to the console output again (this time using System.out). Be wary however, when you use the Reader and Writer classes. They throw the IOException and since this is a checked Exception it must be handled. The code above throws this exception from a method (not shown) and thus need not handle it.