Java 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


//package com.java2s;

import java.io.*;

public class Main {
    /**// w w  w  .jav a2s .c om
     * 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. writeStringToFile(String content, String encoding, String fileName)
  2. writeStringToFile(String content, String filename)
  3. writeStringToFile(String content, String filename)
  4. writeStringToFile(String content, String fileName)
  5. writeStringToFile(String content, String filename)
  6. writeStringToFile(String content, String filePath)
  7. writeStringToFile(String content, String path)
  8. writeStringtoFile(String content, String path)
  9. writeStringToFile(String contents, File outfile)