Java Text File Write by Charset getWriter(String path, Charset encoding)

Here you can find the source of getWriter(String path, Charset encoding)

Description

Returns a writer that uses the given encoding.

License

Open Source License

Parameter

Parameter Description
path the location where the file should be persisted
encoding the character set used for writing the file

Return

a writer that uses the given encoding

Declaration

public static BufferedWriter getWriter(String path, Charset encoding) throws FileNotFoundException 

Method Source Code

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

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    /**//from ww w. j  a  va 2s  . c  o  m
     * Returns a writer that uses the UTF8 encoding.
     * 
     * @param path the location where the file should be persisted
     * @return a reader that uses the UTF8 encoding
     */
    public static BufferedWriter getWriter(String path) throws FileNotFoundException {

        return getWriter(path, StandardCharsets.UTF_8);
    }

    /**
     * Returns a writer that uses the given encoding.
     * 
     * @param path the location where the file should be persisted
     * @param encoding the character set used for writing the file
     * @return a writer that uses the given encoding
     */
    public static BufferedWriter getWriter(String path, Charset encoding) throws FileNotFoundException {

        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path)), encoding));
    }
}

Related

  1. createWriter(@Nullable final OutputStream aOS, @Nonnull final Charset aCharset)
  2. createWriter(@Nullable final OutputStream aOS, @Nonnull final String sCharset)
  3. getWriter(OutputStream os, String charset)
  4. getWriter(OutputStream out, String charsetName)
  5. getWriter(String fileName, Charset cs)
  6. newFilePrintWriter(File file, Charset charset)
  7. newWriter(File file, Charset charset)
  8. newWriter(OutputStream output, Charset encoding)
  9. newWriter(WritableByteChannel ch, Charset cs)