Buffering Writes for Better Performance

The java.io.BufferedWriter class is a subclass of java.io.Writer that you chain to another Writer class to buffer characters. This allows more efficient writing of text.

Each time you write to an unbuffered Writer, there's a matching write to the underlying output stream. Therefore it's a good idea to wrap a BufferedWriter around each Writer whose write() operations are expensive and that does not require immediate response, such as a FileWriter. For example,

BufferedWriter bw = new BufferedWriter(new FileWriter("37.html"));

There are two constructors, one with a default buffer size of 8192 characters, and one that lets you specify the buffer size:

 public BufferedWriter(Writer out)
 public BufferedWriter(Writer out, int size)

The one new method in this class is newLine(). This method that writes a platform dependent line terminator string, \n on Unix, \r on the Mac, \r\n on Windows.

 public String newLine() throws IOException

Previous | Next | Top | Cafe au Lait

Copyright 1997, 2006 Elliotte Rusty Harold
Last Modified August 22, 2006