src/share/classes/java/util/Formatter.java

Print this page




  33 import java.io.FileNotFoundException;
  34 import java.io.Flushable;
  35 import java.io.OutputStream;
  36 import java.io.OutputStreamWriter;
  37 import java.io.PrintStream;
  38 import java.io.UnsupportedEncodingException;
  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.text.DateFormatSymbols;
  45 import java.text.DecimalFormat;
  46 import java.text.DecimalFormatSymbols;
  47 import java.text.NumberFormat;
  48 import java.util.Calendar;
  49 import java.util.Date;
  50 import java.util.Locale;
  51 import java.util.regex.Matcher;
  52 import java.util.regex.Pattern;


  53 
  54 import sun.misc.FpUtils;
  55 import sun.misc.DoubleConsts;
  56 import sun.misc.FormattedFloatingDecimal;
  57 
  58 /**
  59  * An interpreter for printf-style format strings.  This class provides support
  60  * for layout justification and alignment, common formats for numeric, string,
  61  * and date/time data, and locale-specific output.  Common Java types such as
  62  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  63  * are supported.  Limited formatting customization for arbitrary user types is
  64  * provided through the {@link Formattable} interface.
  65  *
  66  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  67  * safety is optional and is the responsibility of users of methods in this
  68  * class.
  69  *
  70  * <p> Formatted printing for the Java language is heavily inspired by C's
  71  * {@code printf}.  Although the format strings are similar to C, some
  72  * customizations have been made to accommodate the Java language and exploit


1821  * </pre></blockquote>
1822  *
1823  * <p> The maximum number of arguments is limited by the maximum dimension of a
1824  * Java array as defined by the <a
1825  * href="http://java.sun.com/docs/books/vmspec/">Java Virtual Machine
1826  * Specification</a>.  If the argument index is does not correspond to an
1827  * available argument, then a {@link MissingFormatArgumentException} is thrown.
1828  *
1829  * <p> If there are more arguments than format specifiers, the extra arguments
1830  * are ignored.
1831  *
1832  * <p> Unless otherwise specified, passing a {@code null} argument to any
1833  * method or constructor in this class will cause a {@link
1834  * NullPointerException} to be thrown.
1835  *
1836  * @author  Iris Clark
1837  * @since 1.5
1838  */
1839 public final class Formatter implements Closeable, Flushable {
1840     private Appendable a;
1841     private Locale l;
1842 
1843     private IOException lastException;
1844 
1845     private char zero = '0';
1846     private static double scaleUp;
1847 
1848     // 1 (sign) + 19 (max # sig digits) + 1 ('.') + 1 ('e') + 1 (sign)
1849     // + 3 (max # exp digits) + 4 (error) = 30
1850     private static final int MAX_FD_CHARS = 30;
1851 
1852     // Initialize internal data.
1853     private void init(Appendable a, Locale l) {
























1854         this.a = a;
1855         this.l = l;
1856         setZero();
1857     }
1858 






1859     /**
1860      * Constructs a new formatter.
1861      *
1862      * <p> The destination of the formatted output is a {@link StringBuilder}
1863      * which may be retrieved by invoking {@link #out out()} and whose
1864      * current content may be converted into a string by invoking {@link
1865      * #toString toString()}.  The locale used is the {@linkplain
1866      * Locale#getDefault() default locale} for this instance of the Java
1867      * virtual machine.
1868      */
1869     public Formatter() {
1870         init(new StringBuilder(), Locale.getDefault(Locale.Category.FORMAT));
1871     }
1872 
1873     /**
1874      * Constructs a new formatter with the specified destination.
1875      *
1876      * <p> The locale used is the {@linkplain Locale#getDefault() default
1877      * locale} for this instance of the Java virtual machine.
1878      *
1879      * @param  a
1880      *         Destination for the formatted output.  If {@code a} is
1881      *         {@code null} then a {@link StringBuilder} will be created.
1882      */
1883     public Formatter(Appendable a) {
1884         if (a == null)
1885             a = new StringBuilder();
1886         init(a, Locale.getDefault(Locale.Category.FORMAT));
1887     }
1888 
1889     /**
1890      * Constructs a new formatter with the specified locale.
1891      *
1892      * <p> The destination of the formatted output is a {@link StringBuilder}
1893      * which may be retrieved by invoking {@link #out out()} and whose current
1894      * content may be converted into a string by invoking {@link #toString
1895      * toString()}.
1896      *
1897      * @param  l
1898      *         The {@linkplain java.util.Locale locale} to apply during
1899      *         formatting.  If {@code l} is {@code null} then no localization
1900      *         is applied.
1901      */
1902     public Formatter(Locale l) {
1903         init(new StringBuilder(), l);
1904     }
1905 
1906     /**
1907      * Constructs a new formatter with the specified destination and locale.
1908      *
1909      * @param  a
1910      *         Destination for the formatted output.  If {@code a} is
1911      *         {@code null} then a {@link StringBuilder} will be created.
1912      *
1913      * @param  l
1914      *         The {@linkplain java.util.Locale locale} to apply during
1915      *         formatting.  If {@code l} is {@code null} then no localization
1916      *         is applied.
1917      */
1918     public Formatter(Appendable a, Locale l) {
1919         if (a == null)
1920             a = new StringBuilder();
1921         init(a, l);
1922     }
1923 
1924     /**
1925      * Constructs a new formatter with the specified file name.
1926      *
1927      * <p> The charset used is the {@linkplain
1928      * java.nio.charset.Charset#defaultCharset() default charset} for this
1929      * instance of the Java virtual machine.
1930      *
1931      * <p> The locale used is the {@linkplain Locale#getDefault() default
1932      * locale} for this instance of the Java virtual machine.
1933      *
1934      * @param  fileName
1935      *         The name of the file to use as the destination of this
1936      *         formatter.  If the file exists then it will be truncated to
1937      *         zero size; otherwise, a new file will be created.  The output
1938      *         will be written to the file and is buffered.
1939      *
1940      * @throws  SecurityException
1941      *          If a security manager is present and {@link
1942      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
1943      *          access to the file
1944      *
1945      * @throws  FileNotFoundException
1946      *          If the given file name does not denote an existing, writable
1947      *          regular file and a new regular file of that name cannot be
1948      *          created, or if some other error occurs while opening or
1949      *          creating the file
1950      */
1951     public Formatter(String fileName) throws FileNotFoundException {
1952         init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
1953              Locale.getDefault(Locale.Category.FORMAT));
1954     }
1955 
1956     /**
1957      * Constructs a new formatter with the specified file name and charset.
1958      *
1959      * <p> The locale used is the {@linkplain Locale#getDefault default
1960      * locale} for this instance of the Java virtual machine.
1961      *
1962      * @param  fileName
1963      *         The name of the file to use as the destination of this
1964      *         formatter.  If the file exists then it will be truncated to
1965      *         zero size; otherwise, a new file will be created.  The output
1966      *         will be written to the file and is buffered.
1967      *
1968      * @param  csn
1969      *         The name of a supported {@linkplain java.nio.charset.Charset
1970      *         charset}
1971      *
1972      * @throws  FileNotFoundException
1973      *          If the given file name does not denote an existing, writable


2008      *         formatting.  If {@code l} is {@code null} then no localization
2009      *         is applied.
2010      *
2011      * @throws  FileNotFoundException
2012      *          If the given file name does not denote an existing, writable
2013      *          regular file and a new regular file of that name cannot be
2014      *          created, or if some other error occurs while opening or
2015      *          creating the file
2016      *
2017      * @throws  SecurityException
2018      *          If a security manager is present and {@link
2019      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
2020      *          access to the file
2021      *
2022      * @throws  UnsupportedEncodingException
2023      *          If the named charset is not supported
2024      */
2025     public Formatter(String fileName, String csn, Locale l)
2026         throws FileNotFoundException, UnsupportedEncodingException
2027     {
2028         init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)),
2029              l);
2030     }
2031 
2032     /**
2033      * Constructs a new formatter with the specified file.
2034      *
2035      * <p> The charset used is the {@linkplain
2036      * java.nio.charset.Charset#defaultCharset() default charset} for this
2037      * instance of the Java virtual machine.
2038      *
2039      * <p> The locale used is the {@linkplain Locale#getDefault() default
2040      * locale} for this instance of the Java virtual machine.
2041      *
2042      * @param  file
2043      *         The file to use as the destination of this formatter.  If the
2044      *         file exists then it will be truncated to zero size; otherwise,
2045      *         a new file will be created.  The output will be written to the
2046      *         file and is buffered.
2047      *
2048      * @throws  SecurityException
2049      *          If a security manager is present and {@link
2050      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2051      *          write access to the file
2052      *
2053      * @throws  FileNotFoundException
2054      *          If the given file object does not denote an existing, writable
2055      *          regular file and a new regular file of that name cannot be
2056      *          created, or if some other error occurs while opening or
2057      *          creating the file
2058      */
2059     public Formatter(File file) throws FileNotFoundException {
2060         init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
2061              Locale.getDefault(Locale.Category.FORMAT));
2062     }
2063 
2064     /**
2065      * Constructs a new formatter with the specified file and charset.
2066      *
2067      * <p> The locale used is the {@linkplain Locale#getDefault default
2068      * locale} for this instance of the Java virtual machine.
2069      *
2070      * @param  file
2071      *         The file to use as the destination of this formatter.  If the
2072      *         file exists then it will be truncated to zero size; otherwise,
2073      *         a new file will be created.  The output will be written to the
2074      *         file and is buffered.
2075      *
2076      * @param  csn
2077      *         The name of a supported {@linkplain java.nio.charset.Charset
2078      *         charset}
2079      *
2080      * @throws  FileNotFoundException
2081      *          If the given file object does not denote an existing, writable


2116      *         formatting.  If {@code l} is {@code null} then no localization
2117      *         is applied.
2118      *
2119      * @throws  FileNotFoundException
2120      *          If the given file object does not denote an existing, writable
2121      *          regular file and a new regular file of that name cannot be
2122      *          created, or if some other error occurs while opening or
2123      *          creating the file
2124      *
2125      * @throws  SecurityException
2126      *          If a security manager is present and {@link
2127      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2128      *          write access to the file
2129      *
2130      * @throws  UnsupportedEncodingException
2131      *          If the named charset is not supported
2132      */
2133     public Formatter(File file, String csn, Locale l)
2134         throws FileNotFoundException, UnsupportedEncodingException
2135     {
2136         init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)),
2137              l);
2138     }
2139 
2140     /**
2141      * Constructs a new formatter with the specified print stream.
2142      *
2143      * <p> The locale used is the {@linkplain Locale#getDefault() default
2144      * locale} for this instance of the Java virtual machine.
2145      *
2146      * <p> Characters are written to the given {@link java.io.PrintStream
2147      * PrintStream} object and are therefore encoded using that object's
2148      * charset.
2149      *
2150      * @param  ps
2151      *         The stream to use as the destination of this formatter.
2152      */
2153     public Formatter(PrintStream ps) {
2154         if (ps == null)
2155             throw new NullPointerException();
2156         init((Appendable)ps, Locale.getDefault(Locale.Category.FORMAT));
2157     }
2158 
2159     /**
2160      * Constructs a new formatter with the specified output stream.
2161      *
2162      * <p> The charset used is the {@linkplain
2163      * java.nio.charset.Charset#defaultCharset() default charset} for this
2164      * instance of the Java virtual machine.
2165      *
2166      * <p> The locale used is the {@linkplain Locale#getDefault() default
2167      * locale} for this instance of the Java virtual machine.
2168      *
2169      * @param  os
2170      *         The output stream to use as the destination of this formatter.
2171      *         The output will be buffered.
2172      */
2173     public Formatter(OutputStream os) {
2174         init(new BufferedWriter(new OutputStreamWriter(os)),
2175              Locale.getDefault(Locale.Category.FORMAT));
2176     }
2177 
2178     /**
2179      * Constructs a new formatter with the specified output stream and
2180      * charset.
2181      *
2182      * <p> The locale used is the {@linkplain Locale#getDefault default
2183      * locale} for this instance of the Java virtual machine.
2184      *
2185      * @param  os
2186      *         The output stream to use as the destination of this formatter.
2187      *         The output will be buffered.
2188      *
2189      * @param  csn
2190      *         The name of a supported {@linkplain java.nio.charset.Charset
2191      *         charset}
2192      *
2193      * @throws  UnsupportedEncodingException
2194      *          If the named charset is not supported
2195      */


2205      *
2206      * @param  os
2207      *         The output stream to use as the destination of this formatter.
2208      *         The output will be buffered.
2209      *
2210      * @param  csn
2211      *         The name of a supported {@linkplain java.nio.charset.Charset
2212      *         charset}
2213      *
2214      * @param  l
2215      *         The {@linkplain java.util.Locale locale} to apply during
2216      *         formatting.  If {@code l} is {@code null} then no localization
2217      *         is applied.
2218      *
2219      * @throws  UnsupportedEncodingException
2220      *          If the named charset is not supported
2221      */
2222     public Formatter(OutputStream os, String csn, Locale l)
2223         throws UnsupportedEncodingException
2224     {
2225         init(new BufferedWriter(new OutputStreamWriter(os, csn)), l);
2226     }
2227 
2228     private void setZero() {
2229         if ((l != null) && !l.equals(Locale.US)) {
2230             DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
2231             zero = dfs.getZeroDigit();


2232         }
2233     }
2234 
2235     /**
2236      * Returns the locale set by the construction of this formatter.
2237      *
2238      * <p> The {@link #format(java.util.Locale,String,Object...) format} method
2239      * for this object which has a locale argument does not change this value.
2240      *
2241      * @return  {@code null} if no localization is applied, otherwise a
2242      *          locale
2243      *
2244      * @throws  FormatterClosedException
2245      *          If this formatter has been closed by invoking its {@link
2246      *          #close()} method
2247      */
2248     public Locale locale() {
2249         ensureOpen();
2250         return l;
2251     }




  33 import java.io.FileNotFoundException;
  34 import java.io.Flushable;
  35 import java.io.OutputStream;
  36 import java.io.OutputStreamWriter;
  37 import java.io.PrintStream;
  38 import java.io.UnsupportedEncodingException;
  39 import java.math.BigDecimal;
  40 import java.math.BigInteger;
  41 import java.math.MathContext;
  42 import java.math.RoundingMode;
  43 import java.nio.charset.Charset;
  44 import java.text.DateFormatSymbols;
  45 import java.text.DecimalFormat;
  46 import java.text.DecimalFormatSymbols;
  47 import java.text.NumberFormat;
  48 import java.util.Calendar;
  49 import java.util.Date;
  50 import java.util.Locale;
  51 import java.util.regex.Matcher;
  52 import java.util.regex.Pattern;
  53 import java.nio.charset.Charset;
  54 import java.nio.charset.IllegalCharsetNameException;
  55 
  56 import sun.misc.FpUtils;
  57 import sun.misc.DoubleConsts;
  58 import sun.misc.FormattedFloatingDecimal;
  59 
  60 /**
  61  * An interpreter for printf-style format strings.  This class provides support
  62  * for layout justification and alignment, common formats for numeric, string,
  63  * and date/time data, and locale-specific output.  Common Java types such as
  64  * {@code byte}, {@link java.math.BigDecimal BigDecimal}, and {@link Calendar}
  65  * are supported.  Limited formatting customization for arbitrary user types is
  66  * provided through the {@link Formattable} interface.
  67  *
  68  * <p> Formatters are not necessarily safe for multithreaded access.  Thread
  69  * safety is optional and is the responsibility of users of methods in this
  70  * class.
  71  *
  72  * <p> Formatted printing for the Java language is heavily inspired by C's
  73  * {@code printf}.  Although the format strings are similar to C, some
  74  * customizations have been made to accommodate the Java language and exploit


1823  * </pre></blockquote>
1824  *
1825  * <p> The maximum number of arguments is limited by the maximum dimension of a
1826  * Java array as defined by the <a
1827  * href="http://java.sun.com/docs/books/vmspec/">Java Virtual Machine
1828  * Specification</a>.  If the argument index is does not correspond to an
1829  * available argument, then a {@link MissingFormatArgumentException} is thrown.
1830  *
1831  * <p> If there are more arguments than format specifiers, the extra arguments
1832  * are ignored.
1833  *
1834  * <p> Unless otherwise specified, passing a {@code null} argument to any
1835  * method or constructor in this class will cause a {@link
1836  * NullPointerException} to be thrown.
1837  *
1838  * @author  Iris Clark
1839  * @since 1.5
1840  */
1841 public final class Formatter implements Closeable, Flushable {
1842     private Appendable a;
1843     private final Locale l;
1844 
1845     private IOException lastException;
1846 
1847     private final char zero;
1848     private static double scaleUp;
1849 
1850     // 1 (sign) + 19 (max # sig digits) + 1 ('.') + 1 ('e') + 1 (sign)
1851     // + 3 (max # exp digits) + 4 (error) = 30
1852     private static final int MAX_FD_CHARS = 30;
1853 
1854     /**
1855      * Returns a charset object for the given charset name.
1856      * @throws NullPointerException          is csn is null
1857      * @throws UnsupportedEncodingException  if the charset is not supported
1858      */
1859     private static Charset toCharset(String csn)
1860             throws UnsupportedEncodingException {
1861         Objects.nonNull(csn, "charsetName");
1862         try {
1863             return Charset.forName(csn);
1864         } catch (IllegalCharsetNameException unused) {
1865             /* swallow this exception since UnsupportedEncodingException
1866              * will be thrown */
1867         }
1868         throw new UnsupportedEncodingException(csn);
1869     }
1870 
1871     private static final Appendable nonNullAppendable(Appendable a) {
1872         if (a == null)
1873             return new StringBuilder();
1874 
1875         return a;
1876     }
1877 
1878     /* Private constructors */
1879     private Formatter(Locale l, Appendable a) {
1880         this.a = a;
1881         this.l = l;
1882         this.zero = getZero(l);
1883     }
1884 
1885     private Formatter(Charset charset, Locale l, File file)
1886             throws FileNotFoundException {
1887         this(l,
1888              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)));
1889     }
1890 
1891     /**
1892      * Constructs a new formatter.
1893      *
1894      * <p> The destination of the formatted output is a {@link StringBuilder}
1895      * which may be retrieved by invoking {@link #out out()} and whose
1896      * current content may be converted into a string by invoking {@link
1897      * #toString toString()}.  The locale used is the {@linkplain
1898      * Locale#getDefault() default locale} for this instance of the Java
1899      * virtual machine.
1900      */
1901     public Formatter() {
1902         this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder());
1903     }
1904 
1905     /**
1906      * Constructs a new formatter with the specified destination.
1907      *
1908      * <p> The locale used is the {@linkplain Locale#getDefault() default
1909      * locale} for this instance of the Java virtual machine.
1910      *
1911      * @param  a
1912      *         Destination for the formatted output.  If {@code a} is
1913      *         {@code null} then a {@link StringBuilder} will be created.
1914      */
1915     public Formatter(Appendable a) {
1916         this(Locale.getDefault(Locale.Category.FORMAT), nonNullAppendable(a));


1917     }
1918 
1919     /**
1920      * Constructs a new formatter with the specified locale.
1921      *
1922      * <p> The destination of the formatted output is a {@link StringBuilder}
1923      * which may be retrieved by invoking {@link #out out()} and whose current
1924      * content may be converted into a string by invoking {@link #toString
1925      * toString()}.
1926      *
1927      * @param  l
1928      *         The {@linkplain java.util.Locale locale} to apply during
1929      *         formatting.  If {@code l} is {@code null} then no localization
1930      *         is applied.
1931      */
1932     public Formatter(Locale l) {
1933         this(l, new StringBuilder());
1934     }
1935 
1936     /**
1937      * Constructs a new formatter with the specified destination and locale.
1938      *
1939      * @param  a
1940      *         Destination for the formatted output.  If {@code a} is
1941      *         {@code null} then a {@link StringBuilder} will be created.
1942      *
1943      * @param  l
1944      *         The {@linkplain java.util.Locale locale} to apply during
1945      *         formatting.  If {@code l} is {@code null} then no localization
1946      *         is applied.
1947      */
1948     public Formatter(Appendable a, Locale l) {
1949         this(l, nonNullAppendable(a));


1950     }
1951 
1952     /**
1953      * Constructs a new formatter with the specified file name.
1954      *
1955      * <p> The charset used is the {@linkplain
1956      * java.nio.charset.Charset#defaultCharset() default charset} for this
1957      * instance of the Java virtual machine.
1958      *
1959      * <p> The locale used is the {@linkplain Locale#getDefault() default
1960      * locale} for this instance of the Java virtual machine.
1961      *
1962      * @param  fileName
1963      *         The name of the file to use as the destination of this
1964      *         formatter.  If the file exists then it will be truncated to
1965      *         zero size; otherwise, a new file will be created.  The output
1966      *         will be written to the file and is buffered.
1967      *
1968      * @throws  SecurityException
1969      *          If a security manager is present and {@link
1970      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
1971      *          access to the file
1972      *
1973      * @throws  FileNotFoundException
1974      *          If the given file name does not denote an existing, writable
1975      *          regular file and a new regular file of that name cannot be
1976      *          created, or if some other error occurs while opening or
1977      *          creating the file
1978      */
1979     public Formatter(String fileName) throws FileNotFoundException {
1980         this(Locale.getDefault(Locale.Category.FORMAT),
1981              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))));
1982     }
1983 
1984     /**
1985      * Constructs a new formatter with the specified file name and charset.
1986      *
1987      * <p> The locale used is the {@linkplain Locale#getDefault default
1988      * locale} for this instance of the Java virtual machine.
1989      *
1990      * @param  fileName
1991      *         The name of the file to use as the destination of this
1992      *         formatter.  If the file exists then it will be truncated to
1993      *         zero size; otherwise, a new file will be created.  The output
1994      *         will be written to the file and is buffered.
1995      *
1996      * @param  csn
1997      *         The name of a supported {@linkplain java.nio.charset.Charset
1998      *         charset}
1999      *
2000      * @throws  FileNotFoundException
2001      *          If the given file name does not denote an existing, writable


2036      *         formatting.  If {@code l} is {@code null} then no localization
2037      *         is applied.
2038      *
2039      * @throws  FileNotFoundException
2040      *          If the given file name does not denote an existing, writable
2041      *          regular file and a new regular file of that name cannot be
2042      *          created, or if some other error occurs while opening or
2043      *          creating the file
2044      *
2045      * @throws  SecurityException
2046      *          If a security manager is present and {@link
2047      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
2048      *          access to the file
2049      *
2050      * @throws  UnsupportedEncodingException
2051      *          If the named charset is not supported
2052      */
2053     public Formatter(String fileName, String csn, Locale l)
2054         throws FileNotFoundException, UnsupportedEncodingException
2055     {
2056         this(toCharset(csn), l, new File(fileName));

2057     }
2058 
2059     /**
2060      * Constructs a new formatter with the specified file.
2061      *
2062      * <p> The charset used is the {@linkplain
2063      * java.nio.charset.Charset#defaultCharset() default charset} for this
2064      * instance of the Java virtual machine.
2065      *
2066      * <p> The locale used is the {@linkplain Locale#getDefault() default
2067      * locale} for this instance of the Java virtual machine.
2068      *
2069      * @param  file
2070      *         The file to use as the destination of this formatter.  If the
2071      *         file exists then it will be truncated to zero size; otherwise,
2072      *         a new file will be created.  The output will be written to the
2073      *         file and is buffered.
2074      *
2075      * @throws  SecurityException
2076      *          If a security manager is present and {@link
2077      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2078      *          write access to the file
2079      *
2080      * @throws  FileNotFoundException
2081      *          If the given file object does not denote an existing, writable
2082      *          regular file and a new regular file of that name cannot be
2083      *          created, or if some other error occurs while opening or
2084      *          creating the file
2085      */
2086     public Formatter(File file) throws FileNotFoundException {
2087         this(Locale.getDefault(Locale.Category.FORMAT),
2088              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))));
2089     }
2090 
2091     /**
2092      * Constructs a new formatter with the specified file and charset.
2093      *
2094      * <p> The locale used is the {@linkplain Locale#getDefault default
2095      * locale} for this instance of the Java virtual machine.
2096      *
2097      * @param  file
2098      *         The file to use as the destination of this formatter.  If the
2099      *         file exists then it will be truncated to zero size; otherwise,
2100      *         a new file will be created.  The output will be written to the
2101      *         file and is buffered.
2102      *
2103      * @param  csn
2104      *         The name of a supported {@linkplain java.nio.charset.Charset
2105      *         charset}
2106      *
2107      * @throws  FileNotFoundException
2108      *          If the given file object does not denote an existing, writable


2143      *         formatting.  If {@code l} is {@code null} then no localization
2144      *         is applied.
2145      *
2146      * @throws  FileNotFoundException
2147      *          If the given file object does not denote an existing, writable
2148      *          regular file and a new regular file of that name cannot be
2149      *          created, or if some other error occurs while opening or
2150      *          creating the file
2151      *
2152      * @throws  SecurityException
2153      *          If a security manager is present and {@link
2154      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2155      *          write access to the file
2156      *
2157      * @throws  UnsupportedEncodingException
2158      *          If the named charset is not supported
2159      */
2160     public Formatter(File file, String csn, Locale l)
2161         throws FileNotFoundException, UnsupportedEncodingException
2162     {
2163         this(toCharset(csn), l, file);

2164     }
2165 
2166     /**
2167      * Constructs a new formatter with the specified print stream.
2168      *
2169      * <p> The locale used is the {@linkplain Locale#getDefault() default
2170      * locale} for this instance of the Java virtual machine.
2171      *
2172      * <p> Characters are written to the given {@link java.io.PrintStream
2173      * PrintStream} object and are therefore encoded using that object's
2174      * charset.
2175      *
2176      * @param  ps
2177      *         The stream to use as the destination of this formatter.
2178      */
2179     public Formatter(PrintStream ps) {
2180         this(Locale.getDefault(Locale.Category.FORMAT),
2181              (Appendable)Objects.nonNull(ps));

2182     }
2183 
2184     /**
2185      * Constructs a new formatter with the specified output stream.
2186      *
2187      * <p> The charset used is the {@linkplain
2188      * java.nio.charset.Charset#defaultCharset() default charset} for this
2189      * instance of the Java virtual machine.
2190      *
2191      * <p> The locale used is the {@linkplain Locale#getDefault() default
2192      * locale} for this instance of the Java virtual machine.
2193      *
2194      * @param  os
2195      *         The output stream to use as the destination of this formatter.
2196      *         The output will be buffered.
2197      */
2198     public Formatter(OutputStream os) {
2199         this(Locale.getDefault(Locale.Category.FORMAT),
2200              new BufferedWriter(new OutputStreamWriter(os)));
2201     }
2202 
2203     /**
2204      * Constructs a new formatter with the specified output stream and
2205      * charset.
2206      *
2207      * <p> The locale used is the {@linkplain Locale#getDefault default
2208      * locale} for this instance of the Java virtual machine.
2209      *
2210      * @param  os
2211      *         The output stream to use as the destination of this formatter.
2212      *         The output will be buffered.
2213      *
2214      * @param  csn
2215      *         The name of a supported {@linkplain java.nio.charset.Charset
2216      *         charset}
2217      *
2218      * @throws  UnsupportedEncodingException
2219      *          If the named charset is not supported
2220      */


2230      *
2231      * @param  os
2232      *         The output stream to use as the destination of this formatter.
2233      *         The output will be buffered.
2234      *
2235      * @param  csn
2236      *         The name of a supported {@linkplain java.nio.charset.Charset
2237      *         charset}
2238      *
2239      * @param  l
2240      *         The {@linkplain java.util.Locale locale} to apply during
2241      *         formatting.  If {@code l} is {@code null} then no localization
2242      *         is applied.
2243      *
2244      * @throws  UnsupportedEncodingException
2245      *          If the named charset is not supported
2246      */
2247     public Formatter(OutputStream os, String csn, Locale l)
2248         throws UnsupportedEncodingException
2249     {
2250         this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));
2251     }
2252 
2253     private static final char getZero(Locale l) {
2254         if ((l != null) && !l.equals(Locale.US)) {
2255             DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
2256             return dfs.getZeroDigit();
2257         } else {
2258             return '0';
2259         }
2260     }
2261 
2262     /**
2263      * Returns the locale set by the construction of this formatter.
2264      *
2265      * <p> The {@link #format(java.util.Locale,String,Object...) format} method
2266      * for this object which has a locale argument does not change this value.
2267      *
2268      * @return  {@code null} if no localization is applied, otherwise a
2269      *          locale
2270      *
2271      * @throws  FormatterClosedException
2272      *          If this formatter has been closed by invoking its {@link
2273      *          #close()} method
2274      */
2275     public Locale locale() {
2276         ensureOpen();
2277         return l;
2278     }