public class BufferedWriter extends Writer
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"));
BufferedReader| Constructor and Description |
|---|
BufferedWriter(Writer out)
Constructs a new
BufferedWriter, providing out with a buffer
of 8192 chars. |
BufferedWriter(Writer out,
int size)
Constructs a new
BufferedWriter, providing out with size chars
of buffer. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this writer.
|
void |
flush()
Flushes this writer.
|
void |
newLine()
Writes a newline to this writer.
|
void |
write(char[] buffer,
int offset,
int count)
Writes
count characters starting at offset in
buffer to this writer. |
void |
write(int oneChar)
Writes the character
oneChar to this writer. |
void |
write(String str,
int offset,
int count)
Writes
count characters starting at offset in str
to this writer. |
public BufferedWriter(Writer out)
BufferedWriter, providing out with a buffer
of 8192 chars.out - the Writer the buffer writes to.public BufferedWriter(Writer out, int size)
BufferedWriter, providing out with size chars
of buffer.out - the OutputStream the buffer writes to.size - the size of buffer in chars.IllegalArgumentException - if size <= 0.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class WriterIOException - if an error occurs while closing this writer.public void flush()
throws IOException
flush in interface Flushableflush in class WriterIOException - if an error occurs while flushing this writer.public void newLine()
throws IOException
"\n".
The target writer may or may not be flushed when a newline is written.IOException - if an error occurs attempting to write to this writer.public void write(char[] buffer,
int offset,
int count)
throws IOException
count characters starting at offset in
buffer 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.write in class Writerbuffer - the array containing characters to write.offset - the start position in buffer for retrieving characters.count - the maximum number of characters to write.IndexOutOfBoundsException - if offset < 0 or count < 0, or if
offset + count is greater than the size of
buffer.IOException - if this writer is closed or another I/O error occurs.public void write(int oneChar)
throws IOException
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.write in class WriteroneChar - the character to write.IOException - if this writer is closed or another I/O error occurs.public void write(String str, int offset, int count) throws IOException
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.write in class Writerstr - the non-null String containing characters to write.offset - the start position in str for retrieving characters.count - maximum number of characters to write.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.