Java Text File Write by Charset writeAll(String data, OutputStream outputStream, Charset charset)

Here you can find the source of writeAll(String data, OutputStream outputStream, Charset charset)

Description

Write string to byte stream

License

Open Source License

Parameter

Parameter Description
data Source string
outputStream Output stream
charset Convert string to bytes according to given Charset

Exception

Parameter Description
IOException an exception

Declaration

public static void writeAll(String data, OutputStream outputStream, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.OutputStream;

import java.nio.charset.Charset;

public class Main {
    private static final String DEFAULT_CHARSET = "UTF-8";

    /**/*from w ww .  j av a2 s . c  o  m*/
     * Write string to byte stream
     * @param data Source string
     * @param outputStream Output stream
     * @param charset Convert string to bytes according to given {@link Charset}
     * @throws IOException
     */
    public static void writeAll(String data, OutputStream outputStream, Charset charset) throws IOException {
        writeAll(data, outputStream, charset.name());
    }

    /**
     * Write string to byte stream
     * @param data Source string
     * @param outputStream Output stream
     * @param charset Convert string to bytes according to given
     * @throws IOException
     */
    public static void writeAll(String data, OutputStream outputStream, String charset) throws IOException {
        if ((data != null) && (data.length() > 0)) {
            outputStream.write(data.getBytes(charset));
        }
    }

    /**
     * Write string to byte stream
     * @param data Source string
     * @param outputStream Output stream
     * @throws IOException
     */
    public static void writeAll(String data, OutputStream outputStream) throws IOException {
        writeAll(data, outputStream, DEFAULT_CHARSET);
    }
}

Related

  1. write(Path file, CharBuffer data, Charset cs)
  2. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  3. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  4. write(String toWrite, File file, Charset charset)
  5. writeAll(Path path, String data, Charset cs)
  6. writeContent(String content, String charset, File file)
  7. writeFile(File file, String content, Charset charset)
  8. writeFile(String content, File file, Charset encoding)
  9. writeFile(String path, String content, Charset charset)