img
Stream Class
Meaning
InputStreamReader
Input stream that translates bytes to characters
LineNumberReader
Input stream that counts lines
OutputStreamWriter
Output stream that translates characters to bytes
PipedReader
Input pipe
PipedWriter
Output pipe
PrintWriter
Output stream that contains print( ) and println( )
PushbackReader
Input stream that allows characters to be returned to the input stream
Reader
Abstract class that describes character stream input
StringReader
Input stream that reads from a string
StringWriter
Output stream that writes to a string
Writer
Abstract class that describes character stream output
TABLE 13-2
The Character Stream I/O Classes (continued)
The Predefined Streams
As you know, all Java programs automatically import the java.lang package. This package
defines a class called System, which encapsulates several aspects of the run-time environment.
For example, using some of its methods, you can obtain the current time and the settings of
various properties associated with the system. System also contains three predefined stream
variables: in, out, and err. These fields are declared as public, static, and final within
System. This means that they can be used by any other part of your program and without
reference to a specific System object.
System.out refers to the standard output stream. By default, this is the console. System.in
refers to standard input, which is the keyboard by default. System.err refers to the standard
error stream, which also is the console by default. However, these streams may be redirected
to any compatible I/O device.
System.in is an object of type InputStream; System.out and System.err are objects of
type PrintStream. These are byte streams, even though they typically are used to read and
write characters from and to the console. As you will see, you can wrap these within character-
based streams, if desired.
The preceding chapters have been using System.out in their examples. You can use
System.err in much the same way. As explained in the next section, use of System.in is
a little more complicated.
Reading Console Input
In Java 1.0, the only way to perform console input was to use a byte stream, and older code
that uses this approach persists. Today, using a byte stream to read console input is still
technically possible, but doing so is not recommended. The preferred method of reading
console input is to use a character-oriented stream, which makes your program easier to
internationalize and maintain.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home