The Console Class

The Console class reads from and writes to the console, if one exists. It implements the Flushable interface.

A Console object is obtained by calling System.console( ), which is shown here:


static Console console( )

If a console is available, then a reference to it is returned. Otherwise, null is returned.

Console defines the methods shown in the following table.

void flush( )
Flush the buffered output.
Console format(String fmtString, Object...args)
format the input.
Console printf(String fmtString, Object...args)
format the input.
Reader reader( )
Returns a reference to a Reader connected to the console.
String readLine( )
Reads and returns a string entered at the keyboard.
String readLine(String fmtString, Object...args)
Displays a prompting string formatted as specified by fmtString and args, and then reads and returns a string entered at the keyboard. Input stops when the user presses ENTER. If the end of the console input stream has been reached, null is returned. An IOError is thrown on failure.
char[ ] readPassword( )
Reads a string entered at the keyboard.
char[ ] readPassword(String fmtString, Object... args)
Displays a prompting string formatted as specified by fmtString and args, and then reads a string entered at the keyboard. Input stops when the user presses ENTER. The string is not displayed. If the end of the console input stream has been reached, null is returned. An IOError is thrown on failure.
PrintWriter writer( )
Returns a reference to a Writer connected to the console.

The following code uses the Console.format(String fmt, Object... args) to do the formatting.


import java.io.Console;
import java.io.IOException;
import java.util.Arrays;

public class Main {

  public static void main(String args[]) throws IOException {

    Console c = System.console();
    if (c == null) {
      System.err.println("No console.");
      System.exit(1);
    }

    String login = c.readLine("Enter your login: ");
    char[] oldPassword = c.readPassword("Enter your old password: ");

    if (verify(login, oldPassword)) {
      boolean noMatch;
      do {
        char[] newPassword1 = c.readPassword("Enter your new password: ");
        char[] newPassword2 = c.readPassword("Enter new password again: ");
        noMatch = !Arrays.equals(newPassword1, newPassword2);
        if (noMatch) {
          c.format("Passwords don't match. Try again.%n");
        } else {
          change(login, newPassword1);
          c.format("Password for %s changed.%n", login);
        }
        Arrays.fill(newPassword1, ' ');
        Arrays.fill(newPassword2, ' ');
      } while (noMatch);
    }

    Arrays.fill(oldPassword, ' ');

  }

  // Dummy verify method.
  static boolean verify(String login, char[] password) {
    return true;
  }

  // Dummy change method.
  static void change(String login, char[] password) {
  }
}
Home 
  Java Book 
    File Stream  

Console:
  1. The Console Class
  2. Console: readLine(String fmt, Object... args)
  3. Console: readPassword(String fmt, Object... args)