Package java.io
Class BufferedWriter
java.lang.Object
java.io.Writer
java.io.BufferedWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
- Direct Known Subclasses:
PemWriter
public class BufferedWriter extends Writer
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:
BufferedReader
-
Field Summary
-
Constructor Summary
Constructors Constructor Description BufferedWriter(Writer out)Constructs a newBufferedWriter, providingoutwith a buffer of 8192 chars.BufferedWriter(Writer out, int size)Constructs a newBufferedWriter, providingoutwithsizechars of buffer. -
Method Summary
Modifier and Type Method Description voidclose()Closes this writer.voidflush()Flushes this writer.voidnewLine()Writes a newline to this writer.voidwrite(char[] buffer, int offset, int count)Writescountcharacters starting atoffsetinbufferto this writer.voidwrite(int oneChar)Writes the characteroneCharto this writer.voidwrite(String str, int offset, int count)Writescountcharacters starting atoffsetinstrto this writer.
-
Constructor Details
-
BufferedWriter
Constructs a newBufferedWriter, providingoutwith a buffer of 8192 chars.- Parameters:
out- theWriterthe buffer writes to.
-
BufferedWriter
Constructs a newBufferedWriter, providingoutwithsizechars of buffer.- Parameters:
out- theOutputStreamthe buffer writes to.size- the size of buffer in chars.- Throws:
IllegalArgumentException- ifsize <= 0.
-
-
Method Details
-
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.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- if an error occurs while closing this writer.
-
flush
Flushes this writer. The contents of the buffer are committed to the target writer and it is then flushed.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- if an error occurs while flushing this writer.
-
newLine
Writes a newline to this writer. On Android, this is"\n". 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.
-
write
Writescountcharacters starting atoffsetinbufferto this writer. Ifcountis greater than this writer's buffer, then the buffer is flushed and the characters are written directly to the target writer.- Specified by:
writein classWriter- Parameters:
buffer- the array containing characters to write.offset- the start position inbufferfor retrieving characters.count- the maximum number of characters to write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the size ofbuffer.IOException- if this writer is closed or another I/O error occurs.
-
write
Writes the characteroneCharto this writer. If the buffer gets full by writing this character, this writer is flushed. Only the lower two bytes of the integeroneCharare written.- Overrides:
writein classWriter- Parameters:
oneChar- the character to write.- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
write
Writescountcharacters starting atoffsetinstrto this writer. Ifcountis 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.- Overrides:
writein classWriter- Parameters:
str- the non-null String containing characters to write.offset- the start position instrfor 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- ifoffset < 0oroffset + countis greater than the length ofstr.
-