Console IO

Some devices don’t have a console, for example a variable speed pump, and others have very minimal ones, for example your microwave oven. A console is typically connected to a keyboard of sorts. In general, consoles are very platform dependent in how they are implemented and how one deals with them. The class java.io.Console is Java’s way of abstracting this concept to do IO to the console in a platform independent way. System.in and System.out will also work, although Console has a nice method for reading and not echoing passwords.

  • public final class Console extends Object implements Flushable

If a virtual machine has a console, it will be represented by a unique object of type Console, and a reference to this object can be obtained by invoking the method System.console(). If no console is available, this method will return null. There is no constructor for Console.

  • Console console = System.console()

Console itself has a number of IO methods

  • void flush() //console.flush() flushes console and forces buffered output to be written immediately
  • Reader reader() //console.reader() returns the unique Reader associated with this console
  • PrintWriter writer() //console.writer() returns the unique PrintWriter associated with this console
  • String readLine(String fmt, Object …args) //console.readLine(…) writes a formatted prompt to console, then reads a single line of text.
  • char[] readPassword(String fmt, Object …args) //console.readPassword(…) writes a formatted prompt and reads a passphrase without echoing it; console.readPassword() does the same without the prompt
  • void format(String fmt, Object … args) //console.format( …) writes a formatted string to the console’s output stream; printf is a synonym

Methods readLine, readPassword, format, and printf can throw IllegalFormatException if the format has illegal syntax or is incompatible with its arguments. There is no close() method. If close() is issued on console.reader() or console.writer() then it has no effect. Similarly a ^D, ^C, or ^Z will return null but will not close the input stream of console, and subsequent characters may be read or written.

The reader() method is useful when one wants to use Scanner on the console via

  • Scanner scanner = new Scanner(console.reader())

Of course, one could just as easily use

  • String line = console.readLine(…)

and then use

  • String[] buf = line.split(regex)

or set up a Scanner on the String line via

  • Scanner scanner = new Scanner(line)

One other detail, System.console() returns null from within an IDE such as Eclipse. A workaround by abstracting console IO is described by McDowell.

The typical startup code using Console looks like:

import java.io.*;
public class ConsoleTest {
  public static void main(String[] args) {
    Console console = System.console();
    if (console != null) {
      boolean check = true;
      String user = null;
      char[] pswd = null;
        while (check) {
          user = console.readLine("Username:  ");
          pswd = console.readPassword("Password:  ");
          check = !checkPassword(user, pswd);
          //choose any verification method...
          pswd = null;
         //so that pswd doesn't stay around very long
          };  //end while
      };  //end if
    //now continue. Either no console exists
    // or username/password is verified
    //more code here...
    };  //end main
  };  //end ConsoleTest

This code will work in an IDE, since the username/password test will be skipped.

Advertisements

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: