Android Text File Write writeStringToFile(String content, String fileName, boolean append)

Here you can find the source of writeStringToFile(String content, String fileName, boolean append)

Description

Writes a string to a file.

License

Open Source License

Parameter

Parameter Description
content the content
fileName the file
append should be the content appended?

Exception

Parameter Description
IOException an exception

Declaration

public static void writeStringToFile(String content, String fileName,
        boolean append) throws IOException 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

public class Main{
    /**/*  w w w  .j  av  a 2s  .com*/
     * Writes a string to a file.
     *
     * @param content  the content
     * @param fileName the file
     * @throws IOException
     */
    public static void writeStringToFile(String content, String fileName)
            throws IOException {
        BufferedWriter fw = createBufferedUtf8Writer(fileName);
        fw.write(content);
        fw.flush();
        fw.close();
    }
    /**
     * Writes a string to a file.
     *
     * @param content  the content
     * @param fileName the file
     * @param append   should be the content appended?
     * @throws IOException
     */
    public static void writeStringToFile(String content, String fileName,
            boolean append) throws IOException {
        BufferedWriter fw = createBufferedUtf8Writer(fileName, append);
        fw.write(content);
        fw.flush();
        fw.close();
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedWriter} using the
     * UTF-8 encoding
     *
     * @param path path to a file to write to
     * @return UTF8 BufferedWriter of the file <tt>path</tt>
     * @throws IOException
     */
    public static BufferedWriter createBufferedUtf8Writer(String path)
            throws IOException {
        return createBufferedUtf8Writer(new File(path));
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedWriter} using the
     * UTF-8 encoding
     *
     * @param path   path to a file to write to
     * @param append should be the content appended?
     * @return UTF8 BufferedWriter of the file <tt>path</tt>
     * @throws IOException
     */
    public static BufferedWriter createBufferedUtf8Writer(String path,
            boolean append) throws IOException {
        return new BufferedWriter(new FileWriter(path, append));
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedWriter} using the
     * UTF-8 encoding
     *
     * @param file file to write to
     * @return UTF8 BufferedWriter of the <tt>file</tt>
     * @throws IOException
     */
    public static BufferedWriter createBufferedUtf8Writer(File file)
            throws IOException {
        return new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), "utf8"));
    }
}

Related

  1. writeLineTextFile(File file, String data)
  2. writeLineTextFile(File file, String[] dataArray)
  3. writeSet2File(Set set, String filePath)
  4. writeString(String string, File file, boolean append)
  5. writeStringToFile(String content, String fileName)
  6. writeStringToFile(String string, File file)
  7. writeStringToFile(String stringToWrite, String filename)
  8. writeText(File file, String text)
  9. writeText(File file, String text, String charsetName)