java.util
public
final
class
java.util.Formatter
Formatter provides the method to give out formatted string just like the
printf-style. Layout,alignment and other format flags are provided to format
numeric,string and date/time as well as locale-specified formats applied.
Besides primitive types, formatter also support some java object types such
as BigInteger,BigDecimal and Calendar. Customized formatting is provided
through the Formattable interface.
The class is not multi-threaded safe. The responsibility to maintain thread
safety is the user's job.
Nested Classes
Summary
Public Constructors
| |
|
|
|
|
Formatter() |
| |
|
|
|
|
Formatter(Appendable a) |
| |
|
|
|
|
Formatter(Locale l) |
| |
|
|
|
|
Formatter(Appendable a, Locale l) |
| |
|
|
|
|
Formatter(String fileName) |
| |
|
|
|
|
Formatter(String fileName, String csn) |
| |
|
|
|
|
Formatter(String fileName, String csn, Locale l) |
| |
|
|
|
|
Formatter(File file) |
| |
|
|
|
|
Formatter(File file, String csn) |
| |
|
|
|
|
Formatter(File file, String csn, Locale l) |
| |
|
|
|
|
Formatter(OutputStream os) |
| |
|
|
|
|
Formatter(OutputStream os, String csn) |
| |
|
|
|
|
Formatter(OutputStream os, String csn, Locale l) |
| |
|
|
|
|
Formatter(PrintStream ps) |
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
Formatter()
Constructs a formatter.
The output is a StringBuilder which can be achieved by invoking the out
method and whose contents can be attained by calling the toString method.
The locale for the formatter is the default locale of the JVM.
Constructs a formatter of which the output is denoted.
The locale for the formatter is the default locale of the JVM.
Parameters
| a
| The output of the formatter. If a is null, then a
StringBuilder will be used.
|
public
Formatter(Locale l)
Constructs a formatter of which the locale is denoted.
The output destination is a StringBuilder which can be achieved by
invoking the out method and whose contents can be attained by calling the
toString method.
Parameters
| l
| The locale of the formatter. If l is null, then no
localization will be used.
|
Constructs a formatter of which the output and locale is denoted.
Parameters
| a
| The output of the formatter. If a is null, then a
StringBuilder will be used. |
| l
| The locale of the formatter. If l is null, then no
localization will be used.
|
public
Formatter(String fileName)
Constructs a formatter of which the filename is denoted.
The charset of the formatter is the default charset of JVM.
The locale for the formatter is the default locale of the JVM.
Parameters
| fileName
| The filename of the file that is used as the output
destination for the formatter. The file will be truncated to
zero size if the file exists, or else a new file will be
created. The output of the formatter is buffered. |
Throws
| FileNotFoundException
| If the filename does not denote a normal and writable file,
or a new file cannot be created or any error rises when
opening or creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()).
|
public
Formatter(String fileName, String csn)
Constructs a formatter of which the filename and charset is denoted.
The locale for the formatter is the default locale of the JVM.
Parameters
| fileName
| The filename of the file that is used as the output
destination for the formatter. The file will be truncated to
zero size if the file exists, or else a new file will be
created. The output of the formatter is buffered. |
| csn
| The name of the charset for the formatter. |
Throws
| FileNotFoundException
| If the filename does not denote a normal and writable file,
or a new file cannot be created or any error rises when
opening or creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()). |
| UnsupportedEncodingException
| If the charset with the specified name is not supported.
|
public
Formatter(String fileName, String csn, Locale l)
Constructs a formatter of which the filename, charset and locale is
denoted.
Parameters
| fileName
| The filename of the file that is used as the output
destination for the formatter. The file will be truncated to
zero size if the file exists, or else a new file will be
created. The output of the formatter is buffered. |
| csn
| The name of the charset for the formatter. |
| l
| The locale of the formatter. If l is null, then no
localization will be used. |
Throws
| FileNotFoundException
| If the filename does not denote a normal and writable file,
or a new file cannot be created or any error rises when
opening or creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()). |
| UnsupportedEncodingException
| If the charset with the specified name is not supported.
|
public
Formatter(File file)
Constructs a formatter of which the file is denoted.
The charset of the formatter is the default charset of JVM.
The locale for the formatter is the default locale of the JVM.
Parameters
| file
| The file that is used as the output destination for the
formatter. The file will be truncated to zero size if the file
exists, or else a new file will be created. The output of the
formatter is buffered. |
Throws
| FileNotFoundException
| If the file does not denote a normal and writable file, or a
new file cannot be created or any error rises when opening or
creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()).
|
public
Formatter(File file, String csn)
Constructs a formatter of which the file and charset is denoted.
The locale for the formatter is the default locale of the JVM.
Parameters
| file
| The file of the file that is used as the output destination
for the formatter. The file will be truncated to zero size if
the file exists, or else a new file will be created. The
output of the formatter is buffered. |
| csn
| The name of the charset for the formatter. |
Throws
| FileNotFoundException
| If the file does not denote a normal and writable file, or a
new file cannot be created or any error rises when opening or
creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()). |
| UnsupportedEncodingException
| If the charset with the specified name is not supported.
|
public
Formatter(File file, String csn, Locale l)
Constructs a formatter of which the file, charset and locale is denoted.
Parameters
| file
| file that is used as the output destination for the formatter.
The file will be truncated to zero size if the file exists, or
else a new file will be created. The output of the formatter
is buffered. |
| csn
| The name of the charset for the formatter. |
| l
| The locale of the formatter. If l is null, then no
localization will be used. |
Throws
| FileNotFoundException
| If the file does not denote a normal and writable file, or a
new file cannot be created or any error rises when opening or
creating the file. |
| SecurityException
| If there is a security manager and it denies writing to the
file in checkWrite(file.getPath()). |
| UnsupportedEncodingException
| If the charset with the specified name is not supported.
|
Constructs a formatter of which the output destination is specified.
The charset of the formatter is the default charset of JVM.
The locale for the formatter is the default locale of the JVM.
Parameters
| os
| The stream used as the destination of the formatter.
|
Constructs a formatter of which the output destination and the charset is
specified.
The locale for the formatter is the default locale of the JVM.
Parameters
| os
| The stream used as the destination of the formatter. |
| csn
| The name of the charset for the formatter. |
Constructs a formatter of which the output destination, the charset and
the locale is specified.
Parameters
| os
| The stream used as the destination of the formatter. |
| csn
| The name of the charset for the formatter. |
| l
| The locale of the formatter. If l is null, then no
localization will be used. |
Constructs a formatter of which the output destination is specified.
The charset of the formatter is the default charset of JVM.
The locale for the formatter is the default locale of the JVM.
Parameters
| ps
| The print stream used as destination of the formatter. If ps
is null, then NullPointerExcepiton will be thrown out.
|
Public Methods
public
void
close()
Closes the formatter. If the output destination is
Closeable,
then the method close() will be called on that destination.
If the formatter has been closed, then calling the close will have no
effect.
Any method but the ioException() that is called after the formatter has
been closed will raise a FormatterClosedException.
public
void
flush()
Flushes the formatter. If the output destination is
Flushable,
then the method flush() will be called on that destination.
Writes a formatted string to the output destination of the formatter.
Parameters
| l
| The locale used in the method. If locale is null, then no
localization will be applied. This parameter does not
influence the locale specified during construction. |
| format
| A format string. |
| args
| The arguments list used in the format() method. If there are
more arguments than those specified by the format string, then
the additional arguments are ignored. |
Throws
| IllegalFormatException
| If the format string is illegal or incompatible with the
arguments or the arguments are less than those required by
the format string or any other illegal situation. |
| FormatterClosedException
| If the formatter has been closed.
|
Writes a formatted string to the output destination of the formatter.
Parameters
| format
| A format string. |
| args
| The arguments list used in the format() method. If there are
more arguments than those specified by the format string, then
the additional arguments are ignored. |
Throws
| IllegalFormatException
| If the format string is illegal or incompatible with the
arguments or the arguments are less than those required by
the format string or any other illegal situation. |
| FormatterClosedException
| If the formatter has been closed.
|
Returns the last IOException thrown out by the formatter's output
destination. If the append() method of the destination will not throw
IOException, the ioException() method will always return null.
Returns
- The last IOException thrown out by the formatter's output
destination.
public
Locale
locale()
Returns the locale of the formatter.
Returns
- The locale for the formatter and null for no locale.
Returns the output destination of the formatter.
Returns
- The output destination of the formatter.
public
String
toString()
Returns the content by calling the toString() method of the output
destination.
Returns
- The content by calling the toString() method of the output
destination.