Java Reference
In-Depth Information
Table 3-4
System Classes
EXAMPLES OF
ASSOCIATED
DEFAULT
CODE
CLASS
FUNCTION
METHODS
DEVICE
EXAMPLE
System.in
Accepts input data from
readLine()
keyboard
String custName = dataIn.readLine();
the keyboard buffer,
used with
buffer
wrapped in the
constructed
InputStreamReader (ISR)
variable
System.out
Sends output to the display
print()
monitor or
System.out.println(“Anita's Antiques”);
or redirects output to a
println()
other display
designated file
flush()
System.err
Sends output to the monitor;
print()
monitor or
System.err.println(“Thread started”);
used for prompts and error
println()
other display
messages
flush()
Recall from Chapter 2 that System.out sent a stream to the standard output
device, which usually is the monitor. System.out implemented the method,
println(), to transfer the stream of characters. When the System.out.println()
method executed, the program sent a literal string argument to the display. In
the code example in Table 3-4, the text, Anita's Antiques, would appear on the
display.
While the Java SDK provides the println() method for output, the SDK con-
tains no simple method for input. System.in actually refers to a buffered input
stream. As a user types data into a program interface, the keystrokes are sent to
a buffer. A buffer is a data area shared by hardware devices or programs, where
data are held until they are needed by the processor. Buffering ensures that if a
user presses the BACKSPACE key to delete a character, the deleted characters are not
sent when the program retrieves the characters from the input buffer.
The java.io package contains several classes used to receive input typed by
the user. One of these classes is the InputStreamReader, which is a special reader
used to read the input buffer. The InputStreamReader (ISR) is a Java class or
object that serves as an intermediary between the input buffer and the Java pro-
gram. Java programmers use the word, wrap , to describe how the ISR envelops
the stream from the input buffer. The Java code necessary to reference the buffer
uses the form
InputStreamReader(System.in)
in which the InputStreamReader() method uses System.in as the argument.
One more step must be completed before a program can use the data from
the ISR. The data must be stored and named in an accessible way, as described in
the next section.
The BufferedReader Class
The BufferedReader class is used to store, or buffer, the input received from
another object or class, such as the InputStreamReader class. The BufferedReader
class, which is part of the java.io package, is used to increase the efficiency of
 
Search WWH ::




Custom Search