• Post Reply Bookmark Topic Watch Topic
  • New Topic

No difference between FileWritter and BufferedWritter  RSS feed

 
W Lin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I run the following code and don't see any difference between FileWritter and BufferedWritter




According to source code, FileWriter.write() eventually calls StreamEncoder.write(), which has "private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192" defined. If so, I don't see any advantage of using BufferedWritter over FileWrite. Do I miss anything?


Thanks!


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W Lin wrote:Hi all,

I run the following code and don't see any difference between FileWritter and BufferedWritter


What difference did you expect to see?

According to source code,


The behavior that we can rely on is not defined by the source code. It's defined by the documentation.

I don't see any advantage of using BufferedWritter over FileWrite.


Either FileWriter is guaranteed to buffer (because its docs say it will), or it is not guaranteed to buffer (because its docs don't say that it does).

If it is guaranteed to buffer, then there's no benefit to adding BufferedWriter here. But if it's not guaranteed to, then we can't rely on what a particular implementation might do, so if we want buffering, we have to provide it ourselves using BufferedWriter.

And finally, remember, there are other Writers we may use besides FileWriter. Even if it buffers, there's no reason to assume any other one will.
 
W Lin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeff,

What difference did you expect to see?


I had thought that, in the sample code I provided, (1) FileWriter would write "b.txt" 10+ times (1024 characters every time), as it has a buffer size 1024 whereas I write 1100 characters every time, and (2) BufferedWriter writes all 11,000 characters to "a.txt" at a time.

The reason I put Thread.sleep(2000) is to watch if b.txt has new characters added to it or not every 2 seconds. But the truth is all characters are added at the same time.

I search the same topic just now and I found that people talked the same thing: BufferedWriter has a larger buffer size, so it is good for writing large chunks of data because it reduces the number of times of writing hard disk:

http://www.coderanch.com/t/431045/java/java/FileWriter-vs-BufferedWriter-FileWriter#1914255

http://stackoverflow.com/questions/6976893/in-java-what-is-the-advantage-of-using-bufferedwriter-to-append-to-a-file


However, my experiment shows that what we thought is wrong. They both buffer all 11,000 characters.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W Lin wrote:Hello Jeff,

What difference did you expect to see?


I had thought that, in the sample code I provided, (1) FileWriter would write "b.txt" 10+ times (1024 characters every time), as it has a buffer size 1024 whereas I write 1100 characters every time, and (2) BufferedWriter writes all 11,000 characters to "a.txt" at a time


Again, you shouldn't assume any particular behavior that isn't documented. Also, I'm not convinced your test will actually measure what you think it does.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!