Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

588 Part II: The Java Library


Here is an example that demonstrates theConsoleclass:

// Demonstrate Console.
import java.io.*;

class ConsoleDemo {
public static void main(String args[]) {
String str;
Console con;

// Obtain a reference to the console.
con = System.console();

Method Description
void flush( ) Causes buffered output to be written physically to the
console.
Console format(StringfmtString,
Object...args)

Writesargsto the console using the format specified
byfmtString.
Console printf(StringfmtString,
Object...args)

Writesargsto the console using the format specified
byfmtString.
Reader reader( ) Returns a reference to aReaderconnected to the
console.
String readLine( ) Reads and returns a string entered at the keyboard.
Input stops when the user pressesENTER. If the end
of the console input stream has been reached,null
is returned. AnIOErroris thrown on failure.
String readLine(StringfmtString,
Object...args)

Displays a prompting string formatted as specified by
fmtStringandargs,and then reads and returns a string
entered at the keyboard. Input stops when the user
pressesENTER. If the end of the console input stream
has been reached,nullis returned. AnIOErroris
thrown on failure.
char[ ] readPassword( ) Reads a string entered at the keyboard. Input stops when
the user pressesENTER. The string is not displayed. If the
end of the console input stream has been reached,nullis
returned. AnIOErroris thrown on failure.
char[ ] readPassword(StringfmtString,
Object...args)

Displays a prompting string formatted as specified
byfmtStringandargs,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,nullis
returned. AnIOErroris thrown on failure.
PrintWriter writer( ) Returns a reference to aWriterconnected to the console.

TABLE 19-5 The Methods Defined byConsole
Free download pdf