Java Reference
In-Depth Information
calls the toString() method for the object to produce the string to be written to the stream. The print()
methods just write the string representation of the argument, whereas the println() method appends \n to
the output. You can create a PrintWriter object from a stream or from another Writer object.
An important point note when using a PrintWriter object is that its methods do not throw I/O excep-
tions. To determine whether any I/O errors have occurred, you have to call the checkError() method for
the PrintWriter object. This method returns true if an error occurred and false otherwise.
The StringWriter and CharArrayWriter classes are for writing character data to a StringBuffer ob-
ject, or an array of type char[] . You typically use these to perform data conversions so that the results are
available to you from the underlying array or string. For example, you could combine the capabilities of a
PrintWriter with a StringWriter to obtain a String object containing binary data converted to charac-
ters:
StringWriter strWriter = new StringWriter();
PrintWriter writer = new PrintWriter(strWriter);
Now you can use the methods for the writer object to write to the StringBuffer object underlying the
StringWriter object:
double value = 2.71828;
writer.println(value);
You can get the result back as a StringBuffer object from the original StringWriter object:
StringBuffer str = strWriter.getBuffer();
Of course, the formatting done by a PrintWriter object does not help make the output line up in
neat columns. If you want that to happen, you have to do it yourself. You see how you might do this for
command-line output a little later in this chapter.
Let's now turn to keyboard input and command-line output.
THE STANDARD STREAMS
Your operating system typically defines three standard streams that are accessible through members of the
System class in Java:
• A standard input stream that usually corresponds to the keyboard by default. This is encapsulated
by the in member of the System class and is of type InputStream .
• A standard output stream that corresponds to output on the command line or in the console win-
dow of an IDE. This is encapsulated by the out member of the System class and is of type
PrintStream .
• A standard error output stream for error messages that usually maps to the command-line output
by default. This is encapsulated by the err member of the System class and is also of type
PrintStream .
You can reassign any of these to another stream within a Java application. The System class provides the
static methods setIn() , setOut() , and setErr() for this purpose. The setIn() method requires an argu-
ment of type InputStream that specifies the new source of standard input. The other two methods expect
an argument of type PrintStream .
Because the standard input stream is of type InputStream , you are not exactly overwhelmed by the cap-
abilities for reading data from the keyboard in Java. Basically, you can read a byte or an array of bytes using
Search WWH ::




Custom Search