Java Reference
In-Depth Information
}
}
}
Of course, the file containing this class definition needs to be in the same directory as the definition for
the FormattedWriter class. If you have typed in the FormattedWriter class and its multitude of
methods correctly, this example should produce the output:
1 1 2 3 5 8
13 21 34 55 89 144
233 377
1.0 1.0 1.414 1.732 2.236
2.828 3.606 4.582 5.831XXXXXXXXXX
one one two three
five eight thirteen
How It Works
We first set up three arrays of different types containing interesting data to write to the command line.
We then create a FormattedWriter object that will write data right-justified in a field width of 12 to a
BufferedWriter object. The BufferedWriter buffers the OutputStreamWriter object that we
wrap around System.out , the stream for output to the command line.
We then exercise our FormattedWriter object by writing each of the arrays to the stream differently.
You can see the effect of exceeding the specified field width with the last value of type double . You
might like to try this with the left-justified option specified in the constructor.
A FormattedWriter object can write to any type of Writer object so you are not limited to just
command line output.
Summary
In this chapter, we have discussed the facilities for inputting and outputting basic types of data to a
stream. The important points we have discussed include:
A stream is an abstract representation of a source of serial input, or a destination for serial output.
The classes supporting stream operations are contained in the package java.io .
Two kinds of stream operations are supported, binary stream operations will result in streams
that contain bytes, and character stream operations are for streams that contain characters in
the local machine character encoding.
No conversion occurs when characters are written to, or read from, a byte stream. Characters
are converted from Unicode to the local machine representation of characters when a
character stream is written.
Byte streams are represented by sub classes of the classes InputStream and
OutputStream .
Search WWH ::




Custom Search