# BufferedWriter

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Inherited Fields](https://developer.android.com/reference/java/io/BufferedWriter#inhfields)\|[Ctors](https://developer.android.com/reference/java/io/BufferedWriter#pubctors)\|[Methods](https://developer.android.com/reference/java/io/BufferedWriter#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/io/BufferedWriter#inhmethods)  

# BufferedWriter

`
public


class
BufferedWriter
`  
`


``

extends `[Writer](https://developer.android.com/reference/java/io/Writer)`


``


`

|---|---|------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.io.Writer](https://developer.android.com/reference/java/io/Writer) ||
|   | ↳ | java.io.BufferedWriter |

<br />

*** ** * ** ***

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:**

- [PrintWriter](https://developer.android.com/reference/java/io/PrintWriter)
- [FileWriter](https://developer.android.com/reference/java/io/FileWriter)
- [OutputStreamWriter](https://developer.android.com/reference/java/io/OutputStreamWriter)
- [Files.newBufferedWriter(Path, Charset, OpenOption)](https://developer.android.com/reference/java/nio/file/Files#newBufferedWriter(java.nio.file.Path,%20java.nio.charset.Charset,%20java.nio.file.OpenOption[]))

## Summary

|                                                                                                                                                                                                                                                    ### Inherited fields                                                                                                                                                                                                                                                    |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From class[java.io.Writer](https://developer.android.com/reference/java/io/Writer) |---------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------| | ` protected `[Object](https://developer.android.com/reference/java/lang/Object) | [lock](https://developer.android.com/reference/java/io/Writer#lock) The object used to synchronize operations on this stream. | |

|                                                                                                                                           ### Public constructors                                                                                                                                            ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[BufferedWriter](https://developer.android.com/reference/java/io/BufferedWriter#BufferedWriter(java.io.Writer))`(`[Writer](https://developer.android.com/reference/java/io/Writer)` out) ` Creates a buffered character-output stream that uses a default-sized output buffer.                         |
| ` `[BufferedWriter](https://developer.android.com/reference/java/io/BufferedWriter#BufferedWriter(java.io.Writer,%20int))`(`[Writer](https://developer.android.com/reference/java/io/Writer)` out, int sz) ` Creates a new buffered character-output stream that uses an output buffer of the given size. |

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

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.io.Writer](https://developer.android.com/reference/java/io/Writer)` ` |----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Writer](https://developer.android.com/reference/java/io/Writer)        | ` `[append](https://developer.android.com/reference/java/io/Writer#append(char))`(char c) ` Appends the specified character to this writer.                                                                                                                                                             | | ` `[Writer](https://developer.android.com/reference/java/io/Writer)        | ` `[append](https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence,%20int,%20int))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` csq, int start, int end) ` Appends a subsequence of the specified character sequence to this writer. | | ` `[Writer](https://developer.android.com/reference/java/io/Writer)        | ` `[append](https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` csq) ` Appends the specified character sequence to this writer.                                                    | | ` abstract void`                                                           | ` `[close](https://developer.android.com/reference/java/io/Writer#close())`() ` Closes the stream, flushing it first.                                                                                                                                                                                   | | ` abstract void`                                                           | ` `[flush](https://developer.android.com/reference/java/io/Writer#flush())`() ` Flushes the stream.                                                                                                                                                                                                     | | ` static `[Writer](https://developer.android.com/reference/java/io/Writer) | ` `[nullWriter](https://developer.android.com/reference/java/io/Writer#nullWriter())`() ` Returns a new`Writer`which discards all characters.                                                                                                                                                           | | ` void`                                                                    | ` `[write](https://developer.android.com/reference/java/io/Writer#write(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` str) ` Writes a string.                                                                                                                | | ` void`                                                                    | ` `[write](https://developer.android.com/reference/java/io/Writer#write(int))`(int c) ` Writes a single character.                                                                                                                                                                                      | | ` void`                                                                    | ` `[write](https://developer.android.com/reference/java/io/Writer#write(java.lang.String,%20int,%20int))`(`[String](https://developer.android.com/reference/java/lang/String)` str, int off, int len) ` Writes a portion of a string.                                                                   | | ` abstract void`                                                           | ` `[write](https://developer.android.com/reference/java/io/Writer#write(char[],%20int,%20int))`(char[] cbuf, int off, int len) ` Writes a portion of an array of characters.                                                                                                                            | | ` void`                                                                    | ` `[write](https://developer.android.com/reference/java/io/Writer#write(char[]))`(char[] cbuf) ` Writes an array of characters.                                                                                                                                                                         | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(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. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(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 void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.lang.Appendable](https://developer.android.com/reference/java/lang/Appendable)` ` |----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract `[Appendable](https://developer.android.com/reference/java/lang/Appendable) | ` `[append](https://developer.android.com/reference/java/lang/Appendable#append(char))`(char c) ` Appends the specified character to this`Appendable`.                                                                                                                                                             | | ` abstract `[Appendable](https://developer.android.com/reference/java/lang/Appendable) | ` `[append](https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence,%20int,%20int))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` csq, int start, int end) ` Appends a subsequence of the specified character sequence to this`Appendable`. | | ` abstract `[Appendable](https://developer.android.com/reference/java/lang/Appendable) | ` `[append](https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` csq) ` Appends the specified character sequence to this`Appendable`.                                                    | ||
| From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| From interface` `[java.io.Flushable](https://developer.android.com/reference/java/io/Flushable)` ` |------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[flush](https://developer.android.com/reference/java/io/Flushable#flush())`() ` Flushes this stream by writing any buffered output to the underlying stream. | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public constructors

### BufferedWriter

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public BufferedWriter (Writer out)
```

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

<br />

|            Parameters            ||
|-------|---------------------------|
| `out` | `Writer`: A Writer <br /> |

### BufferedWriter

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public BufferedWriter (Writer out, 
                int sz)
```

Creates a new buffered character-output stream that uses an output buffer of the given size.

<br />

|                         Parameters                          ||
|-------|------------------------------------------------------|
| `out` | `Writer`: A Writer <br />                            |
| `sz`  | `int`: Output-buffer size, a positive integer <br /> |

|                                                       Throws                                                        ||
|--------------------------------------------------------------------------------------------------------|-------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | If`sz <= 0` |

## Public methods

### close

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

<br />

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

### flush

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void flush ()
```

Flushes the stream.

<br />

|                                               Throws                                               ||
|----------------------------------------------------------------------------|------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | If an I/O error occurs |

### newLine

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

<br />

|                                               Throws                                               ||
|----------------------------------------------------------------------------|------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | If an I/O error occurs |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (int c)
```

Writes a single character.

<br />

|                         Parameters                          ||
|-----|--------------------------------------------------------|
| `c` | `int`: int specifying a character to be written <br /> |

|                                               Throws                                               ||
|----------------------------------------------------------------------------|------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | If an I/O error occurs |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (String s, 
                int off, 
                int len)
```

Writes a portion of a String.

<br />

##### Implementation Requirements:

- While the specification of this method in the[superclass](https://developer.android.com/reference/java/io/Writer#write(java.lang.String,%20int,%20int))recommends that an[IndexOutOfBoundsException](https://developer.android.com/reference/java/lang/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`: String to be written <br />                       |
| `off` | `int`: Offset from which to start reading characters <br /> |
| `len` | `int`: Number of characters to be written <br />            |

|                                                                                          Throws                                                                                           ||
|----------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|
| [IndexOutOfBoundsException](https://developer.android.com/reference/java/lang/IndexOutOfBoundsException) | If`off`is negative, or`off + len`is greater than the length of the given string |
| [IOException](https://developer.android.com/reference/java/io/IOException)                               | If an I/O error occurs                                                          |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

<br />

|                             Parameters                              ||
|--------|-------------------------------------------------------------|
| `cbuf` | `char`: A character array <br />                            |
| `off`  | `int`: Offset from which to start reading characters <br /> |
| `len`  | `int`: Number of characters to write <br />                 |

|                                                                                                          Throws                                                                                                          ||
|----------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| [IndexOutOfBoundsException](https://developer.android.com/reference/java/lang/IndexOutOfBoundsException) | If`off`is negative, or`len`is negative, or`off + len`is negative or greater than the length of the given array |
| [IOException](https://developer.android.com/reference/java/io/IOException)                               | If an I/O error occurs                                                                                         |