Java: What is the difference between StreamWriter and BufferWriter?

eros picture eros · Jun 16, 2011 · Viewed 13.7k times · Source

I am parsing file which is 800MB of size (high possibility of more than 2GB). I split it into several files which approximately 1-3kb per file.

I would like to consult you guys, which is better to use among the two: BufferedWriter and OutputStreamWriter

Any guidance on the right direction is appreciated.

Answer

MJB picture MJB · Jun 16, 2011

Ok, since you ask.

Writer - an abstract class that concrete implementations of let you write characters/strings. As opposed to raw bytes, which OutputStream implementations do.

FileWriter - a concrete implementation that lets you write to a File. Weakness: The encoding of the characters is hard-coded to be the default Locale, for example usually Windows-1252 on Windows, and UTF-8 on Linux.

To overcome this, many people start with an OutputStream (maybe a FileOutputStream) and then convert it into a Writer using OutputStreamWriter, because the constructor lets you set the encoding.

Example:

OutputStream os = new FileOutputStream("turnip");
Writer writer = new OutputStreamWriter(os,"UTF-8");
writer.write("This string will be written as UTF-8");

Now, with OutputStreams/Writers (and their inverse classes InputStream/Readers), it is often useful in addition to wrap a BufferedWriter around them.

continuing from example

writer=new BufferedWriter(writer);
writer.write("Another string in UTF-8");

What does this do? A BufferedWriter basically provides a memory buffer. Everything you write is first stored in memory and then flushed as necessary to disk (or whever). This often provides dramatic performance improvements. To show yourself this, just create a loop of say 100,000 writes without the BufferedWriter, time it, and compare that to the Buffered version.