Here you can find the source of writeFileText(File file, String text)
Parameter | Description |
---|---|
file | file path |
text | text data |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
public static void writeFileText(File file, String text) throws FileNotFoundException, IOException
//package com.java2s; // New BSD License import java.io.BufferedWriter; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; public class Main { private static final String DEFAULT_ENCODING = "UTF8"; /**// w w w . ja v a 2 s. c o m * Writes the text data to the specified output file. * * @param file file path * @param text text data * @throws FileNotFoundException * @throws IOException */ public static void writeFileText(File file, String text) throws FileNotFoundException, IOException { OutputStream out = new FileOutputStream(file); writeLinesCommon(out, text); } private static void writeLinesCommon(OutputStream out, String text) throws IOException { BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(out, DEFAULT_ENCODING)); writer.write(text); } finally { close(writer); } } private static void close(Closeable c) { if (c != null) { try { c.close(); } catch (IOException ex) { } } } }