aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileWriter   vs    BufferedWriter(FileWriter)" Watch "FileWriter   vs    BufferedWriter(FileWriter)" New topic
Author

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?

1:


2:



Thanks a lot,

Suresh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


Regards

Suresh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileWriter vs BufferedWriter(FileWriter)