Java Reference
In-Depth Information
Console Output Using Character Streams
While it is still permissible to use
System.out
to write to the console under Java, its use is
recommended mostly for debugging purposes or for sample programs such as those found
in this topic. For real-world programs, the preferred method of writing to the console when
using Java is through a
PrintWriter
stream.
PrintWriter
is one of the character-based
classes. As explained, using a character-based class for console output makes it easier to
internationalize your program.
PrintWriter
defines several constructors. The one we will use is shown here:
PrintWriter(OutputStream
outputStream
, boolean
flushingOn
)
Here,
outputStream
is an object of type
OutputStream
and
flushingOn
controls whether
Java flushes the output stream every time a
println( )
method (among others) is called. If
flushingOn
is
true
, flushing automatically takes place. If
false
, flushing is not automatic.
PrintWriter
supports the
print( )
and
println( )
methods for all types including
Object
.
Thus, you can use these methods in just the same way as they have been used with
Sys-