Java Reference
In-Depth Information
This method reads the entire input line (up to an end-of-line character) and returns
it as a string.
9.4 Console I/O
The term console typically refers to a command line interface on the host plat-
form such as a cmd or command window when using Windows or Linux shell.
Forabrowser it refers to a window where standard output ( System.out ) and
error messages ( System.err ) from an applet are displayed (no console input
allowed). In this day of graphical interface domination, many consider console
I/O anachronistic. However, Java is intended for many different platforms and
for some systems a console is the most convenient, or perhaps the only way, for a
user to interact with a program. (See, for example, some of the small embedded
processor systems discussed in Chapter 24.) Console I/O also provides a nice
introduction to a number of techniques that apply to all Java I/O.
When the JVM starts, it automatically creates three stream objects accessible
with these static references:
System.in - InputStream object for keyboard input
System.out - PrintStream object for console output
System.err - PrintStream object for console output of error messages
These are all 8-bit streams. The PrintStream constructors have actually been
deprecated but not the class's methods since they are so convenient and com-
mon. As you have seen with our example codes, you can still use System.
out.println() without a deprecation warning message during compilation.
Java 1.0 provided only for 8-bit encoded character I/O. Java 1.1 upgraded
to 16-bit character I/O, but to remain compatible with previous code, the 8-bit
streams remain and are still available for the console and keyboard. The wrapper
classes InputStreamReader and OutputStreamWriter were provided to
convert the 8-bit streams to 16-bit and vice versa.
For binary input, the data is read as bytes and then combined into appropriate
values according to the data types involved. We first discuss the System streams
and then the Reader / Writer streams.
9.4.1 Text output
The following simple program, PrintWriterApp , demonstrates both text out-
put and the wrapping of streams to gain greater capabilities. We first wrap the
System.out object, which is an 8-bit stream, with an OutputStreamWriter
to obtain 16-bit streaming. However, OutputStreamWriter provides only
afew methods. So we wrap our OutputStreamWriter object in turn with
PrintWriter ,which offers a full set of print() and println() methods
for printing strings and primitive types.
Search WWH ::




Custom Search