# OutputStreamWriter

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

|---|---|---|
| Known Direct Subclasses [FileWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileWriter.html) |---|---| | [FileWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileWriter.html) | Convenience class for writing character files. | |||

A class for turning a character stream into a byte stream. Data written to
the target input stream is converted into bytes by either a default or a
provided character converter. The default encoding is taken from the
"file.encoding" system property. `OutputStreamWriter` contains a buffer
of bytes to be written to target stream and converts these into characters as
needed. The buffer size is 8K.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStreamReader.html`

### 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

|---|---|
|   | [OutputStreamWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#OutputStreamWriter(java.io.OutputStream))([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out) Constructs a new OutputStreamWriter using `out` as the target stream to write converted characters to. |
|   | [OutputStreamWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#OutputStreamWriter(java.io.OutputStream,%20java.lang.String))([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) charsetName) Constructs a new OutputStreamWriter using `out` as the target stream to write converted characters to and `charsetName` as the character encoding. |
|   | [OutputStreamWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#OutputStreamWriter(java.io.OutputStream,%20java.nio.charset.Charset))([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [Charset](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/Charset.html) cs) Constructs a new OutputStreamWriter using `out` as the target stream to write converted characters to and `cs` as the character encoding. |
|   | [OutputStreamWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#OutputStreamWriter(java.io.OutputStream,%20java.nio.charset.CharsetEncoder))([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [CharsetEncoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetEncoder.html) charsetEncoder) Constructs a new OutputStreamWriter using `out` as the target stream to write converted characters to and `charsetEncoder` as the character encoder. |

### Public Method Summary

|---|---|
| void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#close())() Closes this writer. |
| void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#flush())() Flushes this writer. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getEncoding](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#getEncoding())() Returns the canonical name of the encoding used by this writer to convert characters to bytes, or null if this writer has been closed. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#write(int))(int oneChar) Writes the character `oneChar` to this writer. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#write(java.lang.String,%20int,%20int))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) str, int offset, int count) Writes `count` characters starting at `offset` in `str` to this writer. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStreamWriter.html#write(char[],%20int,%20int))(char\[\] buffer, int offset, int count) Writes `count` characters starting at `offset` in `buf` to this writer. |

### 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
**OutputStreamWriter**
([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out)

Constructs a new OutputStreamWriter using `out` as the target
stream to write converted characters to. The default character encoding
is used.

##### Parameters

| out | the non-null target stream to write converted bytes to. |
|---|---|

#### public
**OutputStreamWriter**
([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) charsetName)

Constructs a new OutputStreamWriter using `out` as the target
stream to write converted characters to and `charsetName` as the character
encoding. If the encoding cannot be found, an
UnsupportedEncodingException error is thrown.

##### Parameters

| out | the target stream to write converted bytes to. |
| charsetName | the string describing the desired character encoding. |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if `charsetName` is `null`. |
| [UnsupportedEncodingException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/UnsupportedEncodingException.html) | if the encoding specified by `charsetName` cannot be found. |
|---|---|

#### public
**OutputStreamWriter**
([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [Charset](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/Charset.html) cs)

Constructs a new OutputStreamWriter using `out` as the target
stream to write converted characters to and `cs` as the character
encoding.

##### Parameters

| out | the target stream to write converted bytes to. |
| cs | the `Charset` that specifies the character encoding. |
|---|---|

#### public
**OutputStreamWriter**
([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out, [CharsetEncoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetEncoder.html) charsetEncoder)

Constructs a new OutputStreamWriter using `out` as the target
stream to write converted characters to and `charsetEncoder` as the character
encoder.

##### Parameters

| out | the target stream to write converted bytes to. |
| charsetEncoder | the character encoder used for character conversion. |
|---|---|

## Public Methods

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

Closes this writer. This implementation flushes the buffer as well as the
target stream. The target stream is then closed and the resources for the
buffer and converter are released.

Only the first invocation of this method has any effect. Subsequent calls
do nothing.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs while closing this writer. |
|---|---|

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

Flushes this writer. This implementation ensures that all buffered bytes
are written to the target stream. After writing the bytes, the target
stream is flushed as well.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an error occurs while flushing this writer. |
|---|---|

#### public [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**getEncoding**
()

Returns the canonical name of the encoding used by this writer to convert characters to
bytes, or null if this writer has been closed. Most callers should probably keep
track of the String or Charset they passed in; this method may not return the same
name.

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

Writes the character `oneChar` to this writer. The lowest two bytes
of the integer `oneChar` are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
by converting this character, this writer is flushed.

##### Parameters

| oneChar | the character to write. |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if this writer is closed or another I/O error occurs. |
|---|---|

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

Writes `count` characters starting at `offset` in `str`
to this writer. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.

##### Parameters

| str | the string containing characters to write. |
| offset | the start position in `str` for retrieving characters. |
| count | the maximum number of characters to write. |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if this writer has already been closed or another I/O error occurs. |
| [IndexOutOfBoundsException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IndexOutOfBoundsException.html) | if `offset < 0` or `count < 0`, or if `offset + count` is bigger than the length of `str`. |
|---|---|

#### public void
**write**
(char\[\] buffer, int offset, int count)

Writes `count` characters starting at `offset` in `buf`
to this writer. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.

##### Parameters

| buffer | the array containing characters to write. |
| offset | the index of the first character in `buf` to write. |
| count | the maximum number of characters to write. |
|---|---|

##### Throws

| [IndexOutOfBoundsException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IndexOutOfBoundsException.html) | if `offset < 0` or `count < 0`, or if `offset + count` is greater than the size of `buf`. |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if this writer has already been closed or another I/O error occurs. |
|---|---|