< prev index next >

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

Print this page




2120      *          If the given file name 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(fileName)} denies write
2128      *          access to the file
2129      *
2130      * @throws  UnsupportedEncodingException
2131      *          If the named charset is not supported
2132      */
2133     public Formatter(String fileName, String csn, Locale l)
2134         throws FileNotFoundException, UnsupportedEncodingException
2135     {
2136         this(toCharset(csn), l, new File(fileName));
2137     }
2138 
2139     /**

































2140      * Constructs a new formatter with the specified file.
2141      *
2142      * <p> The charset used is the {@linkplain
2143      * java.nio.charset.Charset#defaultCharset() default charset} for this
2144      * instance of the Java virtual machine.
2145      *
2146      * <p> The locale used is the {@linkplain
2147      * Locale#getDefault(Locale.Category) default locale} for
2148      * {@linkplain Locale.Category#FORMAT formatting} for this instance of the Java
2149      * virtual machine.
2150      *
2151      * @param  file
2152      *         The file to use as the destination of this formatter.  If the
2153      *         file exists then it will be truncated to zero size; otherwise,
2154      *         a new file will be created.  The output will be written to the
2155      *         file and is buffered.
2156      *
2157      * @throws  SecurityException
2158      *          If a security manager is present and {@link
2159      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies


2231      *          If the given file object does not denote an existing, writable
2232      *          regular file and a new regular file of that name cannot be
2233      *          created, or if some other error occurs while opening or
2234      *          creating the file
2235      *
2236      * @throws  SecurityException
2237      *          If a security manager is present and {@link
2238      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2239      *          write access to the file
2240      *
2241      * @throws  UnsupportedEncodingException
2242      *          If the named charset is not supported
2243      */
2244     public Formatter(File file, String csn, Locale l)
2245         throws FileNotFoundException, UnsupportedEncodingException
2246     {
2247         this(toCharset(csn), l, file);
2248     }
2249 
2250     /**


































2251      * Constructs a new formatter with the specified print stream.
2252      *
2253      * <p> The locale used is the {@linkplain
2254      * Locale#getDefault(Locale.Category) default locale} for
2255      * {@linkplain Locale.Category#FORMAT formatting} for this instance of the Java
2256      * virtual machine.
2257      *
2258      * <p> Characters are written to the given {@link java.io.PrintStream
2259      * PrintStream} object and are therefore encoded using that object's
2260      * charset.
2261      *
2262      * @param  ps
2263      *         The stream to use as the destination of this formatter.
2264      */
2265     public Formatter(PrintStream ps) {
2266         this(Locale.getDefault(Locale.Category.FORMAT),
2267              (Appendable)Objects.requireNonNull(ps));
2268     }
2269 
2270     /**


2321      * @param  os
2322      *         The output stream to use as the destination of this formatter.
2323      *         The output will be buffered.
2324      *
2325      * @param  csn
2326      *         The name of a supported {@linkplain java.nio.charset.Charset
2327      *         charset}
2328      *
2329      * @param  l
2330      *         The {@linkplain java.util.Locale locale} to apply during
2331      *         formatting.  If {@code l} is {@code null} then no localization
2332      *         is applied.
2333      *
2334      * @throws  UnsupportedEncodingException
2335      *          If the named charset is not supported
2336      */
2337     public Formatter(OutputStream os, String csn, Locale l)
2338         throws UnsupportedEncodingException
2339     {
2340         this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));























2341     }
2342 
2343     private static char getZero(Locale l) {
2344         if ((l != null) && !l.equals(Locale.US)) {
2345             DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
2346             return dfs.getZeroDigit();
2347         } else {
2348             return '0';
2349         }
2350     }
2351 
2352     /**
2353      * Returns the locale set by the construction of this formatter.
2354      *
2355      * <p> The {@link #format(java.util.Locale,String,Object...) format} method
2356      * for this object which has a locale argument does not change this value.
2357      *
2358      * @return  {@code null} if no localization is applied, otherwise a
2359      *          locale
2360      *




2120      *          If the given file name 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(fileName)} denies write
2128      *          access to the file
2129      *
2130      * @throws  UnsupportedEncodingException
2131      *          If the named charset is not supported
2132      */
2133     public Formatter(String fileName, String csn, Locale l)
2134         throws FileNotFoundException, UnsupportedEncodingException
2135     {
2136         this(toCharset(csn), l, new File(fileName));
2137     }
2138 
2139     /**
2140      * Constructs a new formatter with the specified file name, charset, and
2141      * locale.
2142      *
2143      * @param  fileName
2144      *         The name of the file to use as the destination of this
2145      *         formatter.  If the file exists then it will be truncated to
2146      *         zero size; otherwise, a new file will be created.  The output
2147      *         will be written to the file and is buffered.
2148      *
2149      * @param  charset
2150      *         A {@linkplain java.nio.charset.Charset charset}
2151      *
2152      * @param  l
2153      *         The {@linkplain java.util.Locale locale} to apply during
2154      *         formatting.  If {@code l} is {@code null} then no localization
2155      *         is applied.
2156      *
2157      * @throws  IOException
2158      *          if an I/O error occurs while opening or creating the file
2159      *
2160      * @throws  SecurityException
2161      *          If a security manager is present and {@link
2162      *          SecurityManager#checkWrite checkWrite(fileName)} denies write
2163      *          access to the file
2164      *
2165      * @throws NullPointerException
2166      *         if {@code fileName} or {@code charset} is {@code null}.
2167      */
2168     public Formatter(String fileName, Charset charset, Locale l) throws IOException {
2169         this(Objects.requireNonNull(charset, "charset"), l, new File(fileName));
2170     }
2171 
2172     /**
2173      * Constructs a new formatter with the specified file.
2174      *
2175      * <p> The charset used is the {@linkplain
2176      * java.nio.charset.Charset#defaultCharset() default charset} for this
2177      * instance of the Java virtual machine.
2178      *
2179      * <p> The locale used is the {@linkplain
2180      * Locale#getDefault(Locale.Category) default locale} for
2181      * {@linkplain Locale.Category#FORMAT formatting} for this instance of the Java
2182      * virtual machine.
2183      *
2184      * @param  file
2185      *         The file to use as the destination of this formatter.  If the
2186      *         file exists then it will be truncated to zero size; otherwise,
2187      *         a new file will be created.  The output will be written to the
2188      *         file and is buffered.
2189      *
2190      * @throws  SecurityException
2191      *          If a security manager is present and {@link
2192      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies


2264      *          If the given file object does not denote an existing, writable
2265      *          regular file and a new regular file of that name cannot be
2266      *          created, or if some other error occurs while opening or
2267      *          creating the file
2268      *
2269      * @throws  SecurityException
2270      *          If a security manager is present and {@link
2271      *          SecurityManager#checkWrite checkWrite(file.getPath())} denies
2272      *          write access to the file
2273      *
2274      * @throws  UnsupportedEncodingException
2275      *          If the named charset is not supported
2276      */
2277     public Formatter(File file, String csn, Locale l)
2278         throws FileNotFoundException, UnsupportedEncodingException
2279     {
2280         this(toCharset(csn), l, file);
2281     }
2282 
2283     /**
2284      * Constructs a new formatter with the specified file, charset, and
2285      * locale.
2286      *
2287      * @param  file
2288      *         The file to use as the destination of this formatter.  If the
2289      *         file exists then it will be truncated to zero size; otherwise,
2290      *         a new file will be created.  The output will be written to the
2291      *         file and is buffered.
2292      *
2293      * @param  charset
2294      *         A {@linkplain java.nio.charset.Charset charset}
2295      *
2296      * @param  l
2297      *         The {@linkplain java.util.Locale locale} to apply during
2298      *         formatting.  If {@code l} is {@code null} then no localization
2299      *         is applied.
2300      *
2301      * @throws IOException
2302      *         if an I/O error occurs while opening or creating the file
2303      *
2304      * @throws SecurityException
2305      *         If a security manager is present and {@link
2306      *         SecurityManager#checkWrite checkWrite(file.getPath())} denies
2307      *         write access to the file
2308      *
2309      * @throws NullPointerException
2310      *         if {@code file} or {@code charset} is {@code null}.
2311      */
2312     public Formatter(File file, Charset charset, Locale l) throws IOException {
2313         this(Objects.requireNonNull(charset, "charset"), l, file);
2314     }
2315 
2316 
2317     /**
2318      * Constructs a new formatter with the specified print stream.
2319      *
2320      * <p> The locale used is the {@linkplain
2321      * Locale#getDefault(Locale.Category) default locale} for
2322      * {@linkplain Locale.Category#FORMAT formatting} for this instance of the Java
2323      * virtual machine.
2324      *
2325      * <p> Characters are written to the given {@link java.io.PrintStream
2326      * PrintStream} object and are therefore encoded using that object's
2327      * charset.
2328      *
2329      * @param  ps
2330      *         The stream to use as the destination of this formatter.
2331      */
2332     public Formatter(PrintStream ps) {
2333         this(Locale.getDefault(Locale.Category.FORMAT),
2334              (Appendable)Objects.requireNonNull(ps));
2335     }
2336 
2337     /**


2388      * @param  os
2389      *         The output stream to use as the destination of this formatter.
2390      *         The output will be buffered.
2391      *
2392      * @param  csn
2393      *         The name of a supported {@linkplain java.nio.charset.Charset
2394      *         charset}
2395      *
2396      * @param  l
2397      *         The {@linkplain java.util.Locale locale} to apply during
2398      *         formatting.  If {@code l} is {@code null} then no localization
2399      *         is applied.
2400      *
2401      * @throws  UnsupportedEncodingException
2402      *          If the named charset is not supported
2403      */
2404     public Formatter(OutputStream os, String csn, Locale l)
2405         throws UnsupportedEncodingException
2406     {
2407         this(l, new BufferedWriter(new OutputStreamWriter(os, csn)));
2408     }
2409 
2410     /**
2411      * Constructs a new formatter with the specified output stream, charset,
2412      * and locale.
2413      *
2414      * @param  os
2415      *         The output stream to use as the destination of this formatter.
2416      *         The output will be buffered.
2417      *
2418      * @param  charset
2419      *         A {@linkplain java.nio.charset.Charset charset}
2420      *
2421      * @param  l
2422      *         The {@linkplain java.util.Locale locale} to apply during
2423      *         formatting.  If {@code l} is {@code null} then no localization
2424      *         is applied.
2425      *
2426      * @throws NullPointerException
2427      *         if {@code os} or {@code charset} is {@code null}.
2428      */
2429     public Formatter(OutputStream os, Charset charset, Locale l) {
2430         this(l, new BufferedWriter(new OutputStreamWriter(os, charset)));
2431     }
2432 
2433     private static char getZero(Locale l) {
2434         if ((l != null) && !l.equals(Locale.US)) {
2435             DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l);
2436             return dfs.getZeroDigit();
2437         } else {
2438             return '0';
2439         }
2440     }
2441 
2442     /**
2443      * Returns the locale set by the construction of this formatter.
2444      *
2445      * <p> The {@link #format(java.util.Locale,String,Object...) format} method
2446      * for this object which has a locale argument does not change this value.
2447      *
2448      * @return  {@code null} if no localization is applied, otherwise a
2449      *          locale
2450      *


< prev index next >