Java Text File Write writeString(String content, String path, String charset)

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

Description

write String

License

Apache License

Declaration

public static void writeString(String content, String path, String charset) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {

    public static void writeString(String content, String path, String charset) throws IOException {
        PrintWriter writer = null;
        try {//  w  w  w  .j  a v  a  2 s  . c  o m
            writer = getPrintWriter(path, charset, false);
            writer.print(content);
        } finally {
            close(writer);
        }
    }

    public static PrintWriter getPrintWriter(String path, String charset, boolean isAppend) throws IOException {
        return new PrintWriter(getBufferedWriter(path, charset, isAppend));
    }

    public static void close(Closeable closeable) {
        if (closeable == null)
            return;
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }

    public static BufferedWriter getBufferedWriter(String path, String charset, boolean isAppend)
            throws IOException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(touch(path), isAppend), charset));
    }

    public static File touch(String fullFilePath) throws IOException {
        if (fullFilePath == null) {
            return null;
        }
        File file = new File(fullFilePath);

        file.getParentFile().mkdirs();
        if (!file.exists())
            file.createNewFile();
        return file;
    }
}

Related

  1. writeString(final ObjectOutput out, final String s)
  2. writeString(final String data, final int length, final byte[] destination, int offset)
  3. writeString(final String output, final String fileName)
  4. writeString(final String s, final File f)
  5. writeString(String a, String MyFilePath)
  6. writeString(String contents, File file)
  7. writeString(String contents, File file)
  8. writeString(String data, File file)
  9. writeString(String data, String file_path)