Here you can find the source of newWriter(OutputStream output, Charset encoding)
Parameter | Description |
---|---|
output | the output stream |
encoding | the encoding to use when writing to the output stream |
public static Writer newWriter(OutputStream output, Charset encoding)
//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); } }