# BufferedWriter

public class **BufferedWriter** extends [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html)  
Writes text to a character-output stream, buffering characters so as to
provide for the efficient writing of single characters, arrays, and strings.

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,

```
 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
 
```
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/PrintWriter.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileWriter.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#newBufferedWriter(java.nio.file.Path,%20java.nio.charset.Charset,%20java.nio.file.OpenOption...)`

### Inherited Field Summary

From class [java.io.Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html)

|---|---|---|
| protected [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [lock](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#lock) | The object used to synchronize operations on this stream. |

### Public Constructor Summary

|---|---|
|   | [BufferedWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#BufferedWriter(java.io.Writer))([Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) out) Creates a buffered character-output stream that uses a default-sized output buffer. |
|   | [BufferedWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#BufferedWriter(java.io.Writer,%20int))([Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) out, int sz) Creates a new buffered character-output stream that uses an output buffer of the given size. |

### Public Method Summary

|---|---|
| void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#close())() Closes the stream, flushing it first. |
| void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#flush())() Flushes the stream. |
| void | [newLine](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#newLine())() Writes a line separator. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#write(int))(int c) Writes a single character. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#write(java.lang.String,%20int,%20int))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) s, int off, int len) Writes a portion of a String. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/BufferedWriter.html#write(char[],%20int,%20int))(char\[\] cbuf, int off, int len) Writes a portion of an array of characters. |

### Inherited Method Summary

From class [java.io.Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html)

|---|---|
| [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#append(char))(char c) Appends the specified character to this writer. |
| [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#append(java.lang.CharSequence,%20int,%20int))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) csq, int start, int end) Appends a subsequence of the specified character sequence to this writer. |
| [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#append(java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) csq) Appends the specified character sequence to this writer. |
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#close())() Closes the stream, flushing it first. |
| abstract void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#flush())() Flushes the stream. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#write(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) str) Writes a string. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#write(int))(int c) Writes a single character. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#write(java.lang.String,%20int,%20int))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) str, int off, int len) Writes a portion of a string. |
| abstract void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#write(char[],%20int,%20int))(char\[\] cbuf, int off, int len) Writes a portion of an array of characters. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html#write(char[]))(char\[\] cbuf) Writes an array of characters. |

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.lang.Appendable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html)

|---|---|
| abstract [Appendable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html#append(char))(char c) Appends the specified character to this Appendable. |
| abstract [Appendable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html#append(java.lang.CharSequence,%20int,%20int))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) csq, int start, int end) Appends a subsequence of the specified character sequence to this Appendable. |
| abstract [Appendable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html) | [append](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Appendable.html#append(java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) csq) Appends the specified character sequence to this Appendable. |

From interface [java.io.Closeable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |

From interface [java.io.Flushable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Flushable.html)

|---|---|
| abstract void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Flushable.html#flush())() Flushes this stream by writing any buffered output to the underlying stream. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Public Constructors

#### public
**BufferedWriter**
([Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) out)

Creates a buffered character-output stream that uses a default-sized
output buffer.

##### Parameters

| out | A Writer |
|---|---|

#### public
**BufferedWriter**
([Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) out, int sz)

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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If `sz <= 0` |
|---|---|

## Public Methods

#### public void
**close**
()

Closes 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.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) |   |
|---|---|

#### public void
**flush**
()

Flushes the stream.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|

#### public void
**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](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|

#### public void
**write**
(int c)

Writes a single character.

##### Parameters

| c | int specifying a character to be written |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|

#### public void
**write**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) s, int off, int len)

Writes a portion of a String.

If the value of the len parameter is negative then no
characters are written. This is contrary to the specification of this
method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
superclass}, which requires that an `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IndexOutOfBoundsException.html` be
thrown.

##### Parameters

| s | String to be written |
| off | Offset from which to start reading characters |
| len | Number of characters to be written |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|

#### public void
**write**
(char\[\] cbuf, int off, int len)

Writes a portion of an array of characters.

Ordinarily 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
`BufferedWriter`s will not copy data unnecessarily.

##### Parameters

| cbuf | A character array |
| off | Offset from which to start reading characters |
| len | Number of characters to write |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|