public class

BufferedWriter

extends Writer
java.lang.Object
   ↳ java.io.Writer
     ↳ java.io.BufferedWriter

Class Overview

Wraps an existing Writer and buffers the output. Expensive interaction with the underlying reader is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when filling that buffer, but this is usually outweighed by the performance benefits.

A typical application pattern for the class looks like this:

 BufferedWriter buf = new BufferedWriter(new FileWriter("file.java"));
 

See Also

Summary

[Expand]
Inherited Fields
From class java.io.Writer
Public Constructors
BufferedWriter(Writer out)
Constructs a new BufferedWriter with out as the writer for which to buffer write operations.
BufferedWriter(Writer out, int size)
Constructs a new BufferedWriter with out as the writer for which to buffer write operations.
Public Methods
void close()
Closes this writer.
void flush()
Flushes this writer.
void newLine()
Writes a newline to this writer.
void write(char[] cbuf, int offset, int count)
Writes count characters starting at offset in cbuf to this writer.
void write(String str, int offset, int count)
Writes count characters starting at offset in str to this writer.
void write(int oneChar)
Writes the character oneChar to this writer.
[Expand]
Inherited Methods
From class java.io.Writer
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.Appendable

Public Constructors

public BufferedWriter (Writer out)

Constructs a new BufferedWriter with out as the writer for which to buffer write operations. The buffer size is set to the default value of 8 KB.

Parameters
out the writer for which character writing is buffered.

public BufferedWriter (Writer out, int size)

Constructs a new BufferedWriter with out as the writer for which to buffer write operations. The buffer size is set to size.

Parameters
out the writer for which character writing is buffered.
size the size of the buffer in bytes.
Throws
IllegalArgumentException if size <= 0.

Public Methods

public void close ()

Closes this writer. The contents of the buffer are flushed, the target writer is closed, and the buffer is released. Only the first invocation of close has any effect.

Throws
IOException if an error occurs while closing this writer.

public void flush ()

Flushes this writer. The contents of the buffer are committed to the target writer and it is then flushed.

Throws
IOException if an error occurs while flushing this writer.

public void newLine ()

Writes a newline to this writer. A newline is determined by the System property "line.separator". The target writer may or may not be flushed when a newline is written.

Throws
IOException if an error occurs attempting to write to this writer.

public void write (char[] cbuf, int offset, int count)

Writes count characters starting at offset in cbuf to this writer. If count is greater than this writer's buffer, then the buffer is flushed and the characters are written directly to the target writer.

Parameters
cbuf the array containing characters to write.
offset the start position in cbuf for retrieving characters.
count the maximum number of characters to write.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the size of cbuf.
IOException if this writer is closed or another I/O error occurs.

public void write (String str, int offset, int count)

Writes count characters starting at offset in str to this writer. If count is greater than this writer's buffer, then this writer is flushed and the remaining characters are written directly to the target writer. If count is negative no characters are written to the buffer. This differs from the behavior of the superclass.

Parameters
str the non-null String containing characters to write.
offset the start position in str for retrieving characters.
count maximum number of characters to write.
Throws
IOException if this writer has already been closed or another I/O error occurs.
IndexOutOfBoundsException if offset < 0 or offset + count is greater than the length of str.

public void write (int oneChar)

Writes the character oneChar to this writer. If the buffer gets full by writing this character, this writer is flushed. Only the lower two bytes of the integer oneChar are written.

Parameters
oneChar the character to write.
Throws
IOException if this writer is closed or another I/O error occurs.