File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes FileWriter   vs    BufferedWriter(FileWriter) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileWriter   vs    BufferedWriter(FileWriter)" Watch "FileWriter   vs    BufferedWriter(FileWriter)" New topic

FileWriter vs BufferedWriter(FileWriter)

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
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?



Thanks a lot,

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Thank you Ernest for the beautiful explanation. Now I got the idea.


I agree. Here's the link:
subject: FileWriter vs BufferedWriter(FileWriter)
It's not a secret anymore!