Skip navigation links
Java™ Platform
Standard Ed. 8
compact1, compact2, compact3
java.util

Class Formatter

    • Constructor Detail

      • Formatter

        public Formatter()
        Constructs a new formatter.

        The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString(). The locale used is the default locale for formatting for this instance of the Java virtual machine.

      • Formatter

        public Formatter(Appendable a)
        Constructs a new formatter with the specified destination.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        a - Destination for the formatted output. If a is null then a StringBuilder will be created.
      • Formatter

        public Formatter(Locale l)
        Constructs a new formatter with the specified locale.

        The destination of the formatted output is a StringBuilder which may be retrieved by invoking out() and whose current content may be converted into a string by invoking toString().

        Parameters:
        l - The locale to apply during formatting. If l is null then no localization is applied.
      • Formatter

        public Formatter(Appendable a,
                         Locale l)
        Constructs a new formatter with the specified destination and locale.
        Parameters:
        a - Destination for the formatted output. If a is null then a StringBuilder will be created.
        l - The locale to apply during formatting. If l is null then no localization is applied.
      • Formatter

        public Formatter(String fileName)
                  throws FileNotFoundException
        Constructs a new formatter with the specified file name.

        The charset used is the default charset for this instance of the Java virtual machine.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        fileName - The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        Throws:
        SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
        FileNotFoundException - If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
      • Formatter

        public Formatter(String fileName,
                         String csn)
                  throws FileNotFoundException,
                         UnsupportedEncodingException
        Constructs a new formatter with the specified file name and charset.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        fileName - The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        Throws:
        FileNotFoundException - If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
      • Formatter

        public Formatter(String fileName,
                         String csn,
                         Locale l)
                  throws FileNotFoundException,
                         UnsupportedEncodingException
        Constructs a new formatter with the specified file name, charset, and locale.
        Parameters:
        fileName - The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        l - The locale to apply during formatting. If l is null then no localization is applied.
        Throws:
        FileNotFoundException - If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
      • Formatter

        public Formatter(File file)
                  throws FileNotFoundException
        Constructs a new formatter with the specified file.

        The charset used is the default charset for this instance of the Java virtual machine.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        file - The file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        Throws:
        SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
      • Formatter

        public Formatter(File file,
                         String csn)
                  throws FileNotFoundException,
                         UnsupportedEncodingException
        Constructs a new formatter with the specified file and charset.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        file - The file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
      • Formatter

        public Formatter(File file,
                         String csn,
                         Locale l)
                  throws FileNotFoundException,
                         UnsupportedEncodingException
        Constructs a new formatter with the specified file, charset, and locale.
        Parameters:
        file - The file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        l - The locale to apply during formatting. If l is null then no localization is applied.
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
      • Formatter

        public Formatter(PrintStream ps)
        Constructs a new formatter with the specified print stream.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Characters are written to the given PrintStream object and are therefore encoded using that object's charset.

        Parameters:
        ps - The stream to use as the destination of this formatter.
      • Formatter

        public Formatter(OutputStream os)
        Constructs a new formatter with the specified output stream.

        The charset used is the default charset for this instance of the Java virtual machine.

        The locale used is the default locale for formatting for this instance of the Java virtual machine.

        Parameters:
        os - The output stream to use as the destination of this formatter. The output will be buffered.
      • Formatter

        public Formatter(OutputStream os,
                         String csn,
                         Locale l)
                  throws UnsupportedEncodingException
        Constructs a new formatter with the specified output stream, charset, and locale.
        Parameters:
        os - The output stream to use as the destination of this formatter. The output will be buffered.
        csn - The name of a supported charset
        l - The locale to apply during formatting. If l is null then no localization is applied.
        Throws:
        UnsupportedEncodingException - If the named charset is not supported
    • Method Detail

      • locale

        public Locale locale()
        Returns the locale set by the construction of this formatter.

        The format method for this object which has a locale argument does not change this value.

        Returns:
        null if no localization is applied, otherwise a locale
        Throws:
        FormatterClosedException - If this formatter has been closed by invoking its close() method
      • out

        public Appendable out()
        Returns the destination for the output.
        Returns:
        The destination for the output
        Throws:
        FormatterClosedException - If this formatter has been closed by invoking its close() method
      • toString

        public String toString()
        Returns the result of invoking toString() on the destination for the output. For example, the following code formats text into a StringBuilder then retrieves the resultant string:
           Formatter f = new Formatter();
           f.format("Last reboot at %tc", lastRebootDate);
           String s = f.toString();
           // -> s == "Last reboot at Sat Jan 01 00:00:00 PST 2000"
         

        An invocation of this method behaves in exactly the same way as the invocation

             out().toString() 

        Depending on the specification of toString for the Appendable, the returned string may or may not contain the characters written to the destination. For instance, buffers typically return their contents in toString(), but streams cannot since the data is discarded.

        Overrides:
        toString in class Object
        Returns:
        The result of invoking toString() on the destination for the output
        Throws:
        FormatterClosedException - If this formatter has been closed by invoking its close() method
      • flush

        public void flush()
        Flushes this formatter. If the destination implements the Flushable interface, its flush method will be invoked.

        Flushing a formatter writes any buffered output in the destination to the underlying stream.

        Specified by:
        flush in interface Flushable
        Throws:
        FormatterClosedException - If this formatter has been closed by invoking its close() method
      • close

        public void close()
        Closes this formatter. If the destination implements the Closeable interface, its close method will be invoked.

        Closing a formatter allows it to release resources it may be holding (such as open files). If the formatter is already closed, then invoking this method has no effect.

        Attempting to invoke any methods except ioException() in this formatter after it has been closed will result in a FormatterClosedException.

        Specified by:
        close in interface Closeable
        Specified by:
        close in interface AutoCloseable
      • ioException

        public IOException ioException()
        Returns the IOException last thrown by this formatter's Appendable.

        If the destination's append() method never throws IOException, then this method will always return null.

        Returns:
        The last exception thrown by the Appendable or null if no such exception exists.
      • format

        public Formatter format(String format,
                                Object... args)
        Writes a formatted string to this object's destination using the specified format string and arguments. The locale used is the one defined during the construction of this formatter.
        Parameters:
        format - A format string as described in Format string syntax.
        args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.
        Returns:
        This formatter
        Throws:
        IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
        FormatterClosedException - If this formatter has been closed by invoking its close() method
      • format

        public Formatter format(Locale l,
                                String format,
                                Object... args)
        Writes a formatted string to this object's destination using the specified locale, format string, and arguments.
        Parameters:
        l - The locale to apply during formatting. If l is null then no localization is applied. This does not change this object's locale that was set during construction.
        format - A format string as described in Format string syntax
        args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.
        Returns:
        This formatter
        Throws:
        IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
        FormatterClosedException - If this formatter has been closed by invoking its close() method
Java™ Platform
Standard Ed. 8

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.