In Chapter
5: Tech 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 java.util.Formatter
class includes the method
format
(String format, Object... args)
This is virtually identical to the printf()
form in PrintStream
as we discussed
in Chapter 5. The args
parameters will be displayed in the output according to the specifiers
in the format
string in the first parameter.
For a detailed description of the format specifiers,
see the printf()
discussion in Chapter
5: Tech and the Java 2 API Specifications for java.util.Formatter.
The java.util.Formatter
class provides several constructors, each of which includes a
parameter for the destination of the formatted output. Destinations
include OutputStream,
an instance of File,
and any class that implements the new Appendable
interface.
The program FormatWriteApp
shows how we can use Formatter
to send formatted numerical values to the console rather than
using the printf()
method.
FormatWriteApp.java |
import
java.io.*;
import java.util.*;
/**
* Demonstrate the java.util.Formatter capabilities
for
* formatting primitive types.
**/
public class FormatWriteApp
{
public static void main (String arg[]) {
// Send formatted output to the
System.out stream.
Formatter formatter =
new Formatter ((OutputStream)System.out);
formatter.format ("Text output with
Formatter. %n");
formatter.format ("Primitives converted
to strings: %n");
boolean a_boolean = false;
byte a_byte = 114;
short a_short
= 1211;
int an_int = 1234567;
long a_long = 987654321;
float a_float
= 983.6f;
double a_double =
-4.297e-15;
formatter.format ("boolean = %9b
%n", a_boolean);
formatter.format ("byte =
%9d %n", a_byte);
formatter.format ("short
= %9d %n", a_short);
formatter.format ("int
= %9d %n", an_int);
formatter.format ("long =
%9d %n", a_long);
formatter.format ("float
= %9.3f %n", a_float);
formatter.format ("double =
%9.2e %n", a_double);
// Need to flush the data out of
the buffer.
formatter.flush ();
formatter.close ();
} // main
} // class FormatWriteApp
|
The output of this program look like:
In the Formatter
constructor, the System.out
argument, which references an instance of PrintStream,
must be cast to OutputStream
because otherwise there is an ambiguity over which constructor
to use (since PrintStream
implements the Appendable,
which we don't discuss here).
You can directly obtain the formatted string created
by the Formatter
by invoking the toString()
method. Also, if you simply want a formatted string, such as for
a graphical text component, and don't want to send it to an output
destination, you can create a Formatter
with the no-argument constructor. The Formatter
uses internally a StringBuilder
(see Chapter 10) , and you
can access the string that it creates via the toString()
method.
References and Web
Resources
Latest update: Nov. 11, 2004