Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileWriter vs BufferedWriter(FileWriter)

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Could you please let me know the difference between the following 2 piece of codes please. If 1 is correct why should we write unnecessary additional codes in 2? What is the advantage of 2 over 1?

1:


2:



Thanks a lot,

Suresh
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's the full extent of the code, there's no reason to prefer (2). (1) is perfectly fine.

If, on the other hand, you were going to write to the file a number of times -- especially many short writes like a list of a thousand names or something like that -- then using the BufferedWriter will be more efficient. The BufferedWriter will save up many of the little writes and send only large chunks of data to the FileWriter. Writing one large chunk to a file is more efficient than many small ones because each call to FileWriter.write() involves a call to the operating system, and those are slow.
 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest for the beautiful explanation. Now I got the idea.


Regards

Suresh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic