English | Site Directory

Android - An Open Handset Alliance Project

java.util
public final class

java.util.Formatter

java.lang.Object
java.util.Formatter Closeable Flushable

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()
Constructs a formatter.
          Formatter(Appendable a)
Constructs a formatter of which the output is denoted.
          Formatter(Locale l)
Constructs a formatter of which the locale is denoted.
          Formatter(Appendable a, Locale l)
Constructs a formatter of which the output and locale is denoted.
          Formatter(String fileName)
Constructs a formatter of which the filename is denoted.
          Formatter(String fileName, String csn)
Constructs a formatter of which the filename and charset is denoted.
          Formatter(String fileName, String csn, Locale l)
Constructs a formatter of which the filename, charset and locale is denoted.
          Formatter(File file)
Constructs a formatter of which the file is denoted.
          Formatter(File file, String csn)
Constructs a formatter of which the file and charset is denoted.
          Formatter(File file, String csn, Locale l)
Constructs a formatter of which the file, charset and locale is denoted.
          Formatter(OutputStream os)
Constructs a formatter of which the output destination is specified.
          Formatter(OutputStream os, String csn)
Constructs a formatter of which the output destination and the charset is specified.
          Formatter(OutputStream os, String csn, Locale l)
Constructs a formatter of which the output destination, the charset and the locale is specified.
          Formatter(PrintStream ps)
Constructs a formatter of which the output destination is specified.

Public Methods

        void  close()
Closes the formatter.
        void  flush()
Flushes the formatter.
        Formatter  format(Locale l, String format, Object[] args)
Writes a formatted string to the output destination of the formatter.
        Formatter  format(String format, Object[] args)
Writes a formatted string to the output destination of the formatter.
        IOException  ioException()
Returns the last IOException thrown out by the formatter's output destination.
        Locale  locale()
Returns the locale of the formatter.
        Appendable  out()
Returns the output destination of the formatter.
        String  toString()
Returns the content by calling the toString() method of the output destination.
Methods inherited from class java.lang.Object
Methods inherited from interface java.io.Closeable
Methods inherited from interface java.io.Flushable

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.

public Formatter(Appendable a)

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.

public Formatter(Appendable a, Locale l)

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.

public Formatter(OutputStream os)

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.

public Formatter(OutputStream os, String csn)

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.

Throws

UnsupportedEncodingException If the charset with the specified name is not supported.

public Formatter(OutputStream os, String csn, Locale l)

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.

Throws

UnsupportedEncodingException If the charset with the specified name is not supported.

public Formatter(PrintStream ps)

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.

Throws

FormatterClosedException If the formatter has been closed.

public Formatter format(Locale l, String format, Object[] args)

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.

Returns

  • This formatter.

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.

public Formatter format(String format, Object[] args)

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.

Returns

  • This formatter.

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.

public IOException ioException()

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.

Throws

FormatterClosedException If the formatter has been closed.

public Appendable out()

Returns the output destination of the formatter.

Returns

  • The output destination of the formatter.

Throws

FormatterClosedException If the formatter has been closed.

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.

Throws

FormatterClosedException If the formatter has been closed.
Build m5-rc15i - 10 Jun 2008 13:54