Java Text File Write by Charset newWriter(OutputStream output, Charset encoding)

Here you can find the source of newWriter(OutputStream output, Charset encoding)

Description

Creates a java.io.Writer from an output stream

License

Apache License

Parameter

Parameter Description
output the output stream
encoding the encoding to use when writing to the output stream

Return

wrapping the given output stream

Declaration

public static Writer newWriter(OutputStream output, Charset encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 uniVocity Software Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*  w ww  .java 2 s .  co m*/
 ******************************************************************************/

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

public class Main {
    /**
     * Creates a {@link java.io.Writer} from an output stream
     * @param output the output stream
     * @return {@link java.io.Writer} wrapping the given output stream
     */
    public static Writer newWriter(OutputStream output) {
        return newWriter(output, (Charset) null);
    }

    /**
     * Creates a {@link java.io.Writer} from an output stream
     * @param output the output stream
     * @param encoding the encoding to use when writing to the output stream
     * @return {@link java.io.Writer} wrapping the given output stream
     */
    public static Writer newWriter(OutputStream output, String encoding) {
        return newWriter(output, Charset.forName(encoding));
    }

    /**
     * Creates a {@link java.io.Writer} from an output stream
     * @param output the output stream
     * @param encoding the encoding to use when writing to the output stream
     * @return {@link java.io.Writer} wrapping the given output stream
     */
    public static Writer newWriter(OutputStream output, Charset encoding) {
        if (encoding != null) {
            return new OutputStreamWriter(output, encoding);
        } else {
            return new OutputStreamWriter(output);
        }
    }

    /**
     * Creates a {@link java.io.Writer} from a file
     * @param file the file to be written
     * @return {@link java.io.Writer} for the given file
     */
    public static Writer newWriter(File file) {
        return newWriter(file, (Charset) null);
    }

    /**
     * Creates a {@link java.io.Writer} from a file
     * @param file the file to be written
     * @param encoding the encoding to use when writing to the file
     * @return {@link java.io.Writer} for the given file
     */
    public static Writer newWriter(File file, String encoding) {
        return newWriter(file, Charset.forName(encoding));
    }

    /**
     * Creates a {@link java.io.Writer} from a file
     * @param file the file to be written
     * @param encoding the encoding to use when writing to the file
     * @return {@link java.io.Writer} for the given file
     */
    public static Writer newWriter(File file, Charset encoding) {
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new IllegalArgumentException(
                        "Unable to create file '" + file.getAbsolutePath()
                                + "', please ensure your application has permission to create files in that path",
                        e);
            }
        }

        FileOutputStream os;
        try {
            os = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e);
        }

        return newWriter(os, encoding);
    }
}

Related

  1. getWriter(OutputStream out, String charsetName)
  2. getWriter(String fileName, Charset cs)
  3. getWriter(String path, Charset encoding)
  4. newFilePrintWriter(File file, Charset charset)
  5. newWriter(File file, Charset charset)
  6. newWriter(WritableByteChannel ch, Charset cs)
  7. openTextFileForWriting(File f, Charset encoding)
  8. openWriter(OutputStream stream, Charset charset, boolean autoflush)
  9. write(char[] data, File file, String charsetName)