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(); }
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
594684 133059I just put the link of your blog on my Facebook Wall. quite good blog indeed.,-, 136039
363080 725211Could it be okay to write several of this on my small internet website only incorporate a 1 way link towards the web site? 354375
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.
It is in point of fact a nice and useful piece of information. I am glad that you just shared this useful info with us. Please keep us up to date like this. Thank you for sharing.
I like this blog layout . How do you make it? Its so sweet!
Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, as well as the content!
bed sheets that are made of flannel fabric are the best type of bed sheets,
Hey there just stoppin by. Good site you got, keep it going. If you happen to be interested in how to make a beat and have heard about DubTurbo, check out the press release.
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?
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.
I am sure this article has touched all the
internet visitors, its really really fastidious piece of
writing on building up new weblog.
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.
You should take part in a contest for one of the best blogs on the web.
I am going to recommend this web site!
Touche. Solid arguments. Keep up the good effort.
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
Saved as a favorite, I love your blog!
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!
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.
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.
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.
I really like reading through a post that will make people think.
Also, thank you for allowing me to comment!
I used to be able to find good info from your content.
Hurrah! Finally I got a weblog from where I be able to actually get useful data concerning my study and knowledge.
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.
Very soon this web site will be famous among all blogging people,
due to it’s nice content
This is a excellent blog, could you be involved in doing an interview about how you created it? If so e-mail me!
I am continuously looking online for articles that can help me. Thank you!
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
423220 532686quite good goodthis post deserves almost absolutely nothing hahaha merely joking: S nice write-up: P 140699