Java Text File Write by Charset writeFile(String path, String content, Charset charset)

Here you can find the source of writeFile(String path, String content, Charset charset)

Description

write File

License

Apache License

Declaration

public static Boolean writeFile(String path, String content, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

public class Main {
    public static Boolean writeFile(String path) throws IOException {
        return writeFile(path, null);
    }//from   ww  w . jav a2 s  .  c  o m

    public static Boolean writeFile(String path, String content) throws IOException {
        return writeFile(path, content, Charset.forName("UTF-8"));
    }

    public static Boolean writeFile(String path, String content, Charset charset) throws IOException {
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), charset));
            if (content != null) {
                writer.write(content);
            }
        } catch (IOException e) {
            // TODO
            return false;
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                // TODO
                return false;
            }
        }
        return true;
    }
}

Related

  1. writeAll(Path path, String data, Charset cs)
  2. writeAll(String data, OutputStream outputStream, Charset charset)
  3. writeContent(String content, String charset, File file)
  4. writeFile(File file, String content, Charset charset)
  5. writeFile(String content, File file, Charset encoding)
  6. writeFile(String path, String content, Charset encoding)
  7. writeFile(String path, String output, Charset encoding)
  8. writer(OutputStream out, String charset)
  9. writer(String path, String content, String charset)