Java Reference
In-Depth Information
PrintStream
The
PrintStream
class is a concrete decorator for the output stream as shown in Figure
7-6
. It adds the following
functionality to an output stream:
•
It contains methods that let you print any data type values, primitive or object, in a suitable
format for printing.
IOException
. If a method call
throws an
IOException
, it sets an internal flag, rather than throwing the exception to the caller.
The flag can be checked using its
checkError()
method, which returns
true
if an
IOException
occurs during the method execution.
•
Its methods to write data to the output stream do not throw an
•
It has an auto-flush capability. You can specify in its constructor that it should flush the
contents written to it automatically. If you set the auto-flush flag to
true
, it will flush its
contents when a byte array is written, one of its overloaded
println()
methods is used to
write data, a
newline
character is written, or a byte ('
\n
') is written.
Some of the important methods in
PrintStream
class are as follows:
print(Xxx arg)
•
println(Xxx arg)
•
printf()
Here
Xxx
is any primitive data type (
int, char, float
, etc.),
String
, or
Object
.
The
print(Xxx arg)
method writes the specified
arg
value to the output stream in a printable format. For
example, you can use
print(10)
to write an integer to an output stream.
Xxx
also includes two reference types:
String
and
Object
. If your argument is an object, the
toString()
method on that object is called, and the returned string is
written to the output stream. If the object type argument is
null
, a string “null” is written to the output stream. Note
that all input and output streams are byte based. When I mention that the print stream writes a “null” string to the
output stream, it means that the print stream converts the string “null” into bytes and writes those bytes to the output
stream. The character-to-byte conversion is done based on the platform's default character encoding. You can also
provide the character encoding to use for such conversions in some of the constructors of the
PrintStream
class.
The
println(XXX arg)
method works like the
print(XXX arg)
method with one difference. It appends a line
separator string to the specified
arg
. That is, it writes an
arg
value and a line separator to the output stream. The
method
println()
with no argument is used to write a line separator to the output stream. The line separator is
platform dependent and it is determined by the system property
line.separator
.
The
printf()
method is used to write a formatted string to the output stream. For example, if you want to write a
string in the form
"Today is: <<today's date>>"
to a output stream, you can use its
printf()
method as follows:
•
// Assuming that date format is mm/dd/yyyy and ps is the PrintStream object reference
ps.printf("Today is: %1$tm/%1$td/%1$tY", new java.time.LocalDate.now());
Listing 7-19 illustrates how to use a
PrintStream
to write to a file. It writes another stanza from the poem
Lucy
by William Wordsworth to a file named
luci3.txt
. The contents of the file after you run this program would be as
follows:
Upon the moon I fix'd my eye,
All over the wide lea;
With quickening pace my horse drew nigh
Those paths so dear to me.