Java Text File Write by Charset writeString(OutputStream out, String charset, String value)

Here you can find the source of writeString(OutputStream out, String charset, String value)

Description

Convert a \n string to a platform encoding.

License

Apache License

Parameter

Parameter Description
out where to write the converted bytes to
charset the charset to use to convert the raw bytes
value the value to write

Declaration

public static void writeString(OutputStream out, String charset, String value) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.//from w  w  w .  j  av  a2s  . co m
 *******************************************************************************/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.Writer;

import java.nio.charset.Charset;

public class Main {
    public static final Charset UTF8 = Charset.forName("UTF-8");

    /** Convert a \n string to a platform encoding.  This uses a default
     * {@link Charset UTF-8} charset.
     *
     * @param file where to write the converted bytes to
     * @param value the value to write
     */
    public static void writeString(File file, String value) throws IOException {
        writeString(new FileOutputStream(file), UTF8, value);
    }

    /** Convert a \n string to a platform encoding.  This uses a default
     * {@link Charset UTF-8} charset.
     *
     * @param out where to write the converted bytes to
     * @param value the value to write
     */
    public static void writeString(OutputStream out, String value) throws IOException {
        writeString(out, UTF8, value);
    }

    /** Convert a \n string to a platform encoding.  This uses the
     * specified charset to extract the raw bytes.
     *
     * @param out where to write the converted bytes to
     * @param charset the charset to use to convert the raw bytes
     * @param value the value to write
     */
    public static void writeString(OutputStream out, String charset, String value) throws IOException {
        writeString(out, Charset.forName(charset), value);
    }

    /** Convert a \n string to a platform encoding.  This uses the
     * specified charset to extract the raw bytes.
     *
     * @param out where to write the converted bytes to
     * @param charset the charset to use to convert the raw bytes
     * @param value the value to write
     */
    public static void writeString(OutputStream out, Charset charset, String value) throws IOException {
        Writer writer = new OutputStreamWriter(out, charset);
        String nl = System.getProperty("line.separator");
        int r = 0;
        while (r < value.length()) {
            int i = value.indexOf("\n", r);
            if (i == -1) {
                break;
            }
            writer.write(value.substring(r, i));
            writer.write(nl);
            r = i + 1;
        }
        writer.write(value.substring(r));
        writer.close();
    }
}

Related

  1. writer(OutputStream out, String charset)
  2. writer(String path, String content, String charset)
  3. writerBuffered(final File file, final Charset charset)
  4. writeStreamToString(InputStream is, String charsetName)
  5. writeString(OutputStream os, String string, CharsetEncoder encoder)
  6. writeStringToFile(File file, String s, String charset)
  7. writeStringToFile(String fileContent, String filePath, String charset)
  8. writeStringToFile(String filename, String contents, Charset encoding)
  9. writeStringToFile(String fileName, String str, String charset)