Console
In this chapter you will learn:
Use 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.
Next chapter...
What you will learn in the next chapter: