Java Reference
In-Depth Information
if (print
-
writer.checkError ()) {
System.out.println ("An output error occurred!");
}
} // main
} // class PrintWriterApp
The output from this program looks like the following:
Text output with PrintWriter.
Primitives converted to strings:
false
114
1211
1234567
987654321
983.6
-4.297E-15
Note that some wrapper stream classes include a buffer that collects data until it
is full and then the whole buffer data set is sent to its destination in one operation.
This is more efficient than sending each byte one at a time. However, to ensure
that data is not left in a partially filled buffer, a flush method may need to be
invoked. With both
PrintStream
and
PrintWriter
,you have the option of
turning on an
auto-flushing
switch that ensures that no data is left behind.
9.4.2 Numerical output with
Formatter
and
printf()
In Chapter 5 we introduced the new
printf()
method that was added to
the
PrintStream
class by J2SE 5.0. Internally it uses the
java.util.
Formatter
class to format numerical values with specifiers similar to those used
for the
printf()
function in the C language. The
Formatter
class includes
the method
format (String format, Object...args)
This is virtually identical to the
printf()
form that we discussed in Sec-
tion 5.11.2. The
args
parameters are output according to specifiers in the
format
string specified in the first parameter. For details of the format specifiers,
see the
printf()
discussion in Chapter 5 and the Java 2 API Specifications for
java.util.Formatter
[3].
The
java.util.Formatter
class provides several constructors, each
of which includes a parameter for the destination of the formatted output.
Search WWH ::
Custom Search