Class BufferedWriter
The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.
A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.
In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionBufferedWriter(Writer out)Creates a buffered character-output stream that uses a default-sized output buffer.BufferedWriter(Writer out, int sz)Creates a new buffered character-output stream that uses an output buffer of the given size. -
Method Summary
All MethodsInstance MethodsConcrete MethodsModifier and TypeMethodDescriptionvoidclose()Closes the stream, flushing it first.voidflush()Flushes the stream.voidnewLine()Writes a line separator.voidwrite(char[] cbuf, int off, int len)Writes a portion of an array of characters.voidwrite(int c)Writes a single character.voidWrites a portion of a String.Methods declared in class Writer
append, append, append, nullWriter, write, writeModifier and TypeMethodDescriptionappend(char c)Appends the specified character to this writer.append(CharSequence csq)Appends the specified character sequence to this writer.append(CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this writer.static WriterReturns a new Writer which discards all characters.voidwrite(char[] cbuf)Writes an array of characters.voidWrites a string.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?>getClass()Returns the runtime class of this Object.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos)Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Constructor Details
-
BufferedWriter
Creates a buffered character-output stream that uses a default-sized output buffer.Parameters: out - A Writer -
BufferedWriter
Creates a new buffered character-output stream that uses an output buffer of the given size.Parameters: out - A Writer sz - Output-buffer size, a positive integer Throws: IllegalArgumentException - If sz <= 0
-
-
Method Details
-
write
Writes a single character.Overrides: write in class Writer Parameters: c - int specifying a character to be written Throws: IOException - If an I/O error occurs -
write
Writes a portion of an array of characters.Specified by: write in class Writer Parameters: cbuf - A character array off - Offset from which to start reading characters len - Number of characters to write Throws: IndexOutOfBoundsException - If off is negative, or len is negative, or off + len is negative or greater than the length of the given array IOException - If an I/O error occursOrdinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant BufferedWriters will not copy data unnecessarily.
-
write
Writes a portion of a String.Overrides: write in class Writer Implementation Requirements: While the specification of this method in the superclass recommends that an IndexOutOfBoundsException be thrown if len is negative or off + len is negative, the implementation in this class does not throw such an exception in these cases but instead simply writes no characters. Parameters: s - String to be written off - Offset from which to start reading characters len - Number of characters to be written Throws: IndexOutOfBoundsException - If off is negative, or off + len is greater than the length of the given string IOException - If an I/O error occurs -
newLine
Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.Throws: IOException - If an I/O error occurs -
flush
Flushes the stream.Specified by: flush in interface Flushable Specified by: flush in class Writer Throws: IOException - If an I/O error occurs -
close
Description copied from class: WriterCloses the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.Specified by: close in interface AutoCloseable Specified by: close in interface Closeable Specified by: close in class Writer Throws: IOException - If an I/O error occurs
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2026, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.