Here you can find the source of writeStringToFile(String content, String fileName, boolean append)
Parameter | Description |
---|---|
content | the content |
fileName | the file |
append | should be the content appended? |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeStringToFile(String content, String fileName, boolean append) throws IOException
//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")); } }