public class Writereadfiles { private static Logger log = Loggerfactory.getlogger (Writereadfiles.class); public boolean WriteFile (string content) { string filename = "E:/java_src/mvcdemo3/mvcdemo3/accounts.txt"; try { file F = new File (filename); if (!f.exists ()) { f.createnewfile (); } OutputStreamWriter write = new OutputStreamWriter (new FileOutputStream (f)); BufferedWriter writer = new BufferedWriter (write); Writer.write (content); Writer.flush (); Write.close (); Writer.close (); Log.debug ("Write File complete!"); return true; } catch (Exception e) { e.printstacktrace (); } return false; }
Use BufferedWriter to write the content to the file, starting with less wriite.flush () and discovering that the content cannot be written. Check the data found that the original bufferedwriter is buffered input stream, meaning when you call the BufferedWriter write method. The data is written to the buffer and is not written directly to the destination file. You must call the Flush () method of BufferedWriter. This method refreshes the buffer stream, which is where the data is written to the destination file. Must have the appearance of this sentence, in practice if there is no Writer.flush (); Only Writer.close (), will report an exception.
Using BufferedWriter to write data to a file