Java Text File Write by Charset openWriter(OutputStream stream, Charset charset, boolean autoflush)

Here you can find the source of openWriter(OutputStream stream, Charset charset, boolean autoflush)

Description

Returns a PrintWriter attached to the stream with the the specified character set and autoflush.

License

Open Source License

Parameter

Parameter Description
stream the OutputStream to which to write
charset the desired character set
autoflush if true , the println , printf , or format methods will flush the output buffer.

Return

the new PrintWriter

Declaration

public static PrintWriter openWriter(OutputStream stream, Charset charset, boolean autoflush) 

Method Source Code

//package com.java2s;
/**/*w w w  .  jav  a2s  .  com*/
 * Copyright (c) Zachary Kurmas 2011
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.*;
import java.nio.charset.Charset;

import java.util.Map;

public class Main {
    private static final boolean DEFAULT_AUTOFLUSH = false;

    /**
     * Returns a {@code PrintWriter} attached to the {@code stream} with the the specified character set and autoflush.
     *
     * @param stream    the {@code OutputStream} to which to write
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     */
    public static PrintWriter openWriter(OutputStream stream, Charset charset, boolean autoflush) {
        return new PrintWriter(new java.io.BufferedWriter(new java.io.OutputStreamWriter(stream, charset)),
                autoflush);
    }

    /**
    * Returns a {@code PrintWriter} attached to the {@code stream} with the the default character set and autoflush.
    *
    * @param stream    the {@code OutputStream} to which to write
    * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
    *                  output buffer.
    * @return the new {@code PrintWriter}
    */
    public static PrintWriter openWriter(OutputStream stream, boolean autoflush) {
        return openWriter(stream, Charset.defaultCharset(), autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the {@code file} with the the specified character set and autoflush.
     *
     * @param file      the {@code File} to which to write
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(File file, Charset charset, boolean autoflush)
            throws FileNotFoundException {
        return openWriter(new FileOutputStream(file), charset, autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the {@code file} with the the specified character set and autoflush.
     *
     * @param file      the {@code File} to which to write
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(File file, String charset, boolean autoflush)
            throws FileNotFoundException {
        return openWriter(file, Charset.forName(charset), autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the {@code file} with the the default character set specified
     * autoflush.
     *
     * @param file      the {@code File} to which to write
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(File file, boolean autoflush) throws FileNotFoundException {
        return openWriter(file, Charset.defaultCharset(), autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the {@code file} with the the specified character set and without
     * automatic line flushing.
     *
     * @param file    the {@code File} to which to write
     * @param charset the desired character set
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(File file, String charset) throws FileNotFoundException {
        return openWriter(file, charset, DEFAULT_AUTOFLUSH);
    }

    /**
     * Returns a {@code PrintWriter} attached to the file with the the specified character set and autoflush.
     *
     * @param filename  the {@code File} to which to write
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, Charset charset, boolean autoflush)
            throws FileNotFoundException {
        return openWriter(new File(filename), charset, autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the file with the the specified character set and autoflush.
     *
     * @param filename  the {@code File} to which to write
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, String charset, boolean autoflush)
            throws FileNotFoundException {
        return openWriter(filename, Charset.forName(charset), autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the file with the the default character set and specified autoflush.
     *
     * @param filename  the {@code File} to which to write
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, boolean autoflush) throws FileNotFoundException {
        return openWriter(filename, Charset.defaultCharset(), autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to the file with the the specified character set and no automatic line flushing.
     *
     * @param filename the {@code File} to which to write
     * @param charset  the desired character set
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, String charset) throws FileNotFoundException {
        return openWriter(filename, charset, DEFAULT_AUTOFLUSH);
    }

    /**
     * Returns a {@code PrintWriter} attached to either the named file, or the {@code OutputStream} specified in
     * {@code map}  with the the specified character set and autoflush.
     *
     * @param filename  the {@code File} to which to write
     * @param map       a map of filenames to exisiting {@code OutputStreams}.  One use of this feature is to map filenames
     *                  like "-" and "stderr" onto the standard output.
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, Map<String, OutputStream> map, Charset charset,
            boolean autoflush) throws FileNotFoundException {
        return openWriter(getOutputStream(filename, map), charset, autoflush);
    }

    /**
     * Returns a {@code PrintWriter} attached to either the named file, or the {@code OutputStream} specified in
     * {@code map}  with the the specified character set and autoflush.
     *
     * @param filename  the {@code File} to which to write
     * @param map       a map of filenames to exisiting {@code OutputStreams}.  One use of this feature is to map filenames
     *                  like "-" and "stderr" onto the standard output.
     * @param charset   the desired character set
     * @param autoflush if {@code true}, the {@code println}, {@code printf}, or {@code format} methods will flush the
     *                  output buffer.
     * @return the new {@code PrintWriter}
     * @throws FileNotFoundException if {@code file} cannot be opened for writing.
     */
    public static PrintWriter openWriter(String filename, Map<String, OutputStream> map, String charset,
            boolean autoflush) throws FileNotFoundException {
        return openWriter(filename, map, Charset.forName(charset), autoflush);
    }

    /**
     * Returns the {@code OutputStream} contained in the {@code map}, if present,
     * or creates a new {@code OutputStream }attached to the specified file.  {@link #DEFAULT_OUTPUT_STREAM_MAP} maps
     * common names for the standard output and standard error to {@code System.out} and {@code System.err} respectively.
     *
     * @param filename the name of the file to open, or one of the keys in {@code map}.
     * @param map      a map of names to existing {@code OutputStreams}
     * @return either the {@code OutputStream} in the map, or a new {@code OutputStream}.
     * @throws FileNotFoundException if the requested file does not exist.
     */
    public static OutputStream getOutputStream(String filename, Map<String, OutputStream> map)
            throws FileNotFoundException {
        if (map != null && map.containsKey(filename)) {
            return map.get(filename);
        } else {
            return new FileOutputStream(filename);
        }
    }
}

Related

  1. newFilePrintWriter(File file, Charset charset)
  2. newWriter(File file, Charset charset)
  3. newWriter(OutputStream output, Charset encoding)
  4. newWriter(WritableByteChannel ch, Charset cs)
  5. openTextFileForWriting(File f, Charset encoding)
  6. write(char[] data, File file, String charsetName)
  7. write(File file, Charset charset, String content)
  8. write(File file, Object content, Charset cs, boolean append)
  9. write(final String s, final OutputStream out, Charset charset)