I’m looking at the following example
Which uses the following code
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
}
catch (IOException e) {}
What’s the advantage over doing
FileWriter fw = new FileWriter("outfilename");
I have tried both and they seem comparable in speed when it comes to the task of appending to a file one line at a time
The Javadoc provides a reasonable discussion on this subject:
If you’re writing large blocks of text at once (like entire lines) then you probably won’t notice a difference. If you have a lot of code that appends a single character at a time, however, a
BufferedWriterwill be much more efficient.Edit
As per andrew’s comment below, the
FileWriteractually uses its own fixed-size 1024 byte buffer. This was confirmed by looking at the source code. TheBufferedWritersources, on the other hand, show that it uses and 8192 byte buffer size (default), which can be configured by the user to any other desired size. So it seems like the benefits ofBufferedWritervs.FileWriterare limited to:And to further muddy the waters, the Java 6 implementation of
OutputStreamWriteractually delegates to a StreamEncoder, which uses its own buffer with a default size of 8192 bytes. And theStreamEncoderbuffer is user-configurable, although there is no way to access it directly through the enclosingOutputStreamWriter.