• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileWriter vs BufferedWriter(FileWriter)

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ernest for the beautiful explanation. Now I got the idea.


Regards

Suresh.
reply
    Bookmark Topic Watch Topic
  • New Topic