The Formatter Constructors
Before you can use Formatter to format output, you must create a Formatter object. In general,
Formatter works by converting the binary form of data used by a program into formatted
text. It stores the formatted text in a buffer, the contents of which can be obtained by your
program whenever they are needed. It is possible to let Formatter supply this buffer
automatically, or you can specify the buffer explicitly when a Formatter object is created.
It is also possible to have Formatter output its buffer to a file.
The Formatter class defines many constructors, which enable you to construct a Formatter
in a variety of ways. Here is a sampling:
Formatter( )
Formatter(Appendable buf)
Formatter(Appendable buf, Locale loc)
Formatter(String filename)
throws FileNotFoundException
Formatter(String filename, String charset)
throws FileNotFoundException, UnsupportedEncodingException
Formatter(File outF)
throws FileNotFoundException
Formatter(OutputStream outStrm)
Here, buf specifies a buffer for the formatted output. If buf is null, then Formatter automatically
allocates a StringBuilder to hold the formatted output. The loc parameter specifies a locale.
If no locale is specified, the default locale is used. The filename parameter specifies the name
of a file that will receive the formatted output. The charset parameter specifies the character
set. If no character set is specified, then the default character set is used. The outF parameter
specifies a reference to an open file that will receive output. The outStrm parameter specifies
a reference to an output stream that will receive output. When using a file, output is also
written to the file.
Perhaps the most widely used constructor is the first, which has no parameters.
It automatically uses the default locale and allocates a StringBuilder to hold the formatted
output.
The Formatter Methods
Formatter defines the methods shown in Table 18-11.
Formatting Basics
After you have created a Formatter, you can use it to create a formatted string. To do so, use
the format( ) method. The most commonly used version is shown here:
Formatter format(String fmtString, Object ... args)
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home