Here you can find the source of writeStringToFile(String content, String fileName)
Parameter | Description |
---|---|
content | the content |
fileName | the file |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeStringToFile(String content, String fileName) throws IOException
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{ /**//from ww w . ja v a 2 s. co m * 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")); } }