How to write Text File in Java by BufferedWriter

Java provides a BufferedWriter for writting content to File with high performance when performing with large number of write action. Because BufferedWriter decreases I/O interaction by using internal buffer to write data into File.
Related posts:
1. Java Read Text File by BufferedReader, Java 7,Java 8
2. How to delete non-empty Folder in Java

I. BufferedWriter – Classis Approach (Java5, Java6)
String FILE_NAME = "C:\\test\\test.txt";
try {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME));
    StringBuffer content = new StringBuffer();
    content.append("Line 1");
    content.append(System.getProperty("line.separator"));
    content.append("Line 2");
    content.append(System.getProperty("line.separator"));
    content.append("Line 3");
    content.append(System.getProperty("line.separator"));
    content.append("Line 4");
    content.append(System.getProperty("line.separator"));
    content.append("Line 5");
    content.append(System.getProperty("line.separator"));

    // write content
    bufferedWriter.write(content.toString());
    // close BufferedWriter
    bufferedWriter.close();
    System.out.println("Done");
} catch (IOException e) {
    e.printStackTrace();
}

Now C:\\test\\test.txt file with content:

Line 1
Line 2
Line 3
Line 4
Line 5

For append data to an existed File, In constructor: public FileWriter(String fileName, boolean append)
set append = true
new BufferedWriter(new FileWriter(FILE_NAME,true))

try {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME,true));
    StringBuffer content = new StringBuffer();
    content.append("Line 6");
    content.append(System.getProperty("line.separator"));
    content.append("Line 7");
    content.append(System.getProperty("line.separator"));
    content.append("Line 8");
    content.append(System.getProperty("line.separator"));
    content.append("Line 9");
    content.append(System.getProperty("line.separator"));
    content.append("Line 10");
    content.append(System.getProperty("line.separator"));
   
    // write content
    bufferedWriter.write(content.toString());
    // close BufferedWriter
    bufferedWriter.close();
    System.out.println("Done");
} catch (IOException e) {
    e.printStackTrace();
}

Content of C:\\test\\test.txt file:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
II. BufferedWriter – Java 7 Approach

Implement With JAVA 7: use try-catch-resource
-> No need to CLOSE the buferedWriter after processing.

try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME))){
	StringBuffer content = new StringBuffer();
	content.append("Line 1");
    content.append(System.getProperty("line.separator"));
    content.append("Line 2");
    content.append(System.getProperty("line.separator"));
    content.append("Line 3");
    content.append(System.getProperty("line.separator"));
    content.append("Line 4");
    content.append(System.getProperty("line.separator"));
    content.append("Line 5");
    content.append(System.getProperty("line.separator"));
    bufferedWriter.write(content.toString());
    System.out.println("Done");
}catch(IOException e){
	e.printStackTrace();
}

Code for Append content:

//
// Append content to a FILE using BufferedWriter
// In constructor: public FileWriter(String fileName, boolean append)
// -> set append = true
//
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FILE_NAME, true))){
	StringBuffer content = new StringBuffer();
	content.append("Line 6");
    content.append(System.getProperty("line.separator"));
    content.append("Line 7");
    content.append(System.getProperty("line.separator"));
    content.append("Line 8");
    content.append(System.getProperty("line.separator"));
    content.append("Line 9");
    content.append(System.getProperty("line.separator"));
    content.append("Line 10");
    content.append(System.getProperty("line.separator"));
    bufferedWriter.write(content.toString());
    System.out.println("Done");
}catch(IOException e){
	e.printStackTrace();
}
III. Sourcecode

Write2FileJava

30 thoughts on “How to write Text File in Java by BufferedWriter”

  1. There couple of fascinating points on time in this article but I do not determine if I see these people center to heart. You can find some validity but I am going to take hold opinion until I check into it further. Excellent write-up , thanks and we want a lot more! Combined with FeedBurner too

    Rankcircles.com

  2. The next occasion I read a weblog, Lets hope it doesnt disappoint me as much as that one. I am talking about, I know it was my method to read, but I really thought youd have some thing fascinating to mention. All I hear is actually a number of whining about something you could fix if you ever werent too busy searching for attention.

  3. magnificent issues altogether, you simply gained a logo new reader.

    What might you recommend in regards to your post that you simply
    made some days ago? Any positive?

  4. I’m amazed, I must say. Seldom do I come across a blog that’s
    both equally educative and interesting, and without a doubt, you have hit the nail on the head.
    The problem is something that not enough folks are speaking intelligently about.
    Now i’m very happy that I came across this in my
    search for something regarding this.

  5. I am sure this article has touched all the
    internet visitors, its really really fastidious piece of
    writing on building up new weblog.

  6. I’ve been exploring for a little bit for any high quality articles or blog
    posts in this sort of area . Exploring in Yahoo I finally stumbled upon this website.
    Reading this information So i’m satisfied to express that I’ve an incredibly good uncanny feeling I found out exactly what I needed.
    I so much definitely will make certain to do not put out of your mind this website and provides it
    a look regularly.

  7. I enjoy, lead to I found just what I used to be looking for.
    You have ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

  8. Please let me know if you’re looking for a article
    author for your blog. You have some really good posts and
    I think I would be a good asset. If you ever want to take some of the load off, I’d love to write some material for your blog
    in exchange for a link back to mine. Please send me
    an email if interested. Cheers!

  9. My relatives all the time say that I am wasting
    my time here at web, however I know I am getting knowledge everyday by
    reading such nice articles.

  10. Hi to every body, it’s my first go to see of this weblog;
    this web site consists of amazing and really good data in support of readers.

  11. Excellent post. I used to be checking continuously this blog and I am inspired!

    Extremely helpful info specially the closing phase 🙂 I deal with such info a lot.
    I used to be seeking this particular info for a long time.
    Thanks and good luck.

  12. Hello! I know this is somewhat off topic but I was wondering which blog platform are you
    using for this site? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform.
    I would be fantastic if you could point me in the direction of a good platform.

  13. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *