Using BufferedWriter to write data to a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.