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/OutputStreamWriter#inhfields) \| [Ctors](https://developer.android.com/reference/java/io/OutputStreamWriter#pubctors) \| [Methods](https://developer.android.com/reference/java/io/OutputStreamWriter#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/io/OutputStreamWriter#inhmethods)

# OutputStreamWriter

` public class OutputStreamWriter `   
` ` ` extends 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.OutputStreamWriter |

Known direct subclasses [FileWriter](https://developer.android.com/reference/java/io/FileWriter) [FileWriter](https://developer.android.com/reference/java/io/FileWriter) Writes text to character files using a default buffer size.   

*** ** * ** ***

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified `https://developer.android.com/reference/java/nio/charset/Charset`. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. Note that the characters passed to the write() methods are not buffered.

For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:

```
Writer out
  = new BufferedWriter(new OutputStreamWriter(System.out));
```

A *surrogate pair* is a character represented by a sequence of two `char` values: A *high* surrogate in the range '\\uD800' to '\\uDBFF' followed by a *low* surrogate in the range '\\uDC00' to '\\uDFFF'.

A *malformed surrogate element* is a high surrogate that is not followed by a low surrogate or a low surrogate that is not preceded by a high surrogate.

This class always replaces malformed surrogate elements and unmappable character sequences with the charset's default *substitution sequence*. The [CharsetEncoder](https://developer.android.com/reference/java/nio/charset/CharsetEncoder) class should be used when more control over the encoding process is required.
**See also:**

- `https://developer.android.com/reference/java/io/BufferedWriter`
- `https://developer.android.com/reference/java/io/OutputStream`
- `https://developer.android.com/reference/java/nio/charset/Charset`

## Summary

### Inherited fields

From class `https://developer.android.com/reference/java/io/Writer` ` protected https://developer.android.com/reference/java/lang/Object` `https://developer.android.com/reference/java/io/Writer#lock`

The object used to synchronize operations on this stream.

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/io/OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)(https://developer.android.com/reference/java/io/OutputStream out) ` Creates an OutputStreamWriter that uses the default character encoding. |
| ` https://developer.android.com/reference/java/io/OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,%20java.lang.String)(https://developer.android.com/reference/java/io/OutputStream out, https://developer.android.com/reference/java/lang/String charsetName) ` Creates an OutputStreamWriter that uses the named charset. |
| ` https://developer.android.com/reference/java/io/OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,%20java.nio.charset.Charset)(https://developer.android.com/reference/java/io/OutputStream out, https://developer.android.com/reference/java/nio/charset/Charset cs) ` Creates an OutputStreamWriter that uses the given charset. |
| ` https://developer.android.com/reference/java/io/OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,%20java.nio.charset.CharsetEncoder)(https://developer.android.com/reference/java/io/OutputStream out, https://developer.android.com/reference/java/nio/charset/CharsetEncoder enc) ` Creates an OutputStreamWriter that uses the given charset encoder. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/io/Writer` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#append(java.lang.CharSequence,%20int,%20int)(https://developer.android.com/reference/java/lang/CharSequence csq, int start, int end) ` Appends a subsequence of the specified character sequence to this writer. |
| ` https://developer.android.com/reference/java/io/Writer` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) ` Appends the specified character sequence to this writer. |
| ` void` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#close()() ` Closes the stream, flushing it first. |
| ` void` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#flush()() ` Flushes the stream. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#getEncoding()() ` Returns the name of the character encoding being used by this stream. |
| ` void` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(int)(int c) ` Writes a single character. |
| ` void` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(java.lang.String,%20int,%20int)(https://developer.android.com/reference/java/lang/String str, int off, int len) ` Writes a portion of a string. |
| ` void` | ` https://developer.android.com/reference/java/io/OutputStreamWriter#write(char[],%20int,%20int)(char[] cbuf, int off, int len) ` Writes a portion of an array of characters. |

### Inherited methods

From class ` https://developer.android.com/reference/java/io/Writer ` ` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(char)(char c) `

Appends the specified character to this writer.
` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence,%20int,%20int)(https://developer.android.com/reference/java/lang/CharSequence csq, int start, int end) `

Appends a subsequence of the specified character sequence to this writer.
` https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) `

Appends the specified character sequence to this writer.
` abstract void` ` https://developer.android.com/reference/java/io/Writer#close()() `

Closes the stream, flushing it first.
` abstract void` ` https://developer.android.com/reference/java/io/Writer#flush()() `

Flushes the stream.
` static https://developer.android.com/reference/java/io/Writer` ` https://developer.android.com/reference/java/io/Writer#nullWriter()() `

Returns a new `Writer` which discards all characters.
` void` ` https://developer.android.com/reference/java/io/Writer#write(java.lang.String)(https://developer.android.com/reference/java/lang/String str) `

Writes a string.
` void` ` https://developer.android.com/reference/java/io/Writer#write(int)(int c) `

Writes a single character.
` void` ` https://developer.android.com/reference/java/io/Writer#write(java.lang.String,%20int,%20int)(https://developer.android.com/reference/java/lang/String str, int off, int len) `

Writes a portion of a string.
` abstract void` ` 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` ` https://developer.android.com/reference/java/io/Writer#write(char[])(char[] cbuf) `

Writes an array of characters.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` 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 https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` 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` ` 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` ` 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 ` https://developer.android.com/reference/java/lang/Appendable ` ` abstract https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(char)(char c) `

Appends the specified character to this `Appendable`.
` abstract https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence,%20int,%20int)(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 https://developer.android.com/reference/java/lang/Appendable` ` https://developer.android.com/reference/java/lang/Appendable#append(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence csq) `

Appends the specified character sequence to this `Appendable`.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/java/io/Flushable ` ` abstract void` ` https://developer.android.com/reference/java/io/Flushable#flush()() `

Flushes this stream by writing any buffered output to the underlying stream.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Public constructors

### OutputStreamWriter

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

```
public OutputStreamWriter (OutputStream out)
```

Creates an OutputStreamWriter that uses the default character encoding.

<br />

| Parameters ||
|---|---|
| `out` | `OutputStream`: An OutputStream <br /> |

### OutputStreamWriter

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

```
public OutputStreamWriter (OutputStream out, 
                String charsetName)
```

Creates an OutputStreamWriter that uses the named charset.

<br />

| Parameters ||
|---|---|
| `out` | `OutputStream`: An OutputStream <br /> |
| `charsetName` | `String`: The name of a supported `https://developer.android.com/reference/java/nio/charset/Charset` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/UnsupportedEncodingException` | If the named encoding is not supported |

### OutputStreamWriter

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

```
public OutputStreamWriter (OutputStream out, 
                Charset cs)
```

Creates an OutputStreamWriter that uses the given charset.

<br />

| Parameters ||
|---|---|
| `out` | `OutputStream`: An OutputStream <br /> |
| `cs` | `Charset`: A charset <br /> |

### OutputStreamWriter

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

```
public OutputStreamWriter (OutputStream out, 
                CharsetEncoder enc)
```

Creates an OutputStreamWriter that uses the given charset encoder.

<br />

| Parameters ||
|---|---|
| `out` | `OutputStream`: An OutputStream <br /> |
| `enc` | `CharsetEncoder`: A charset encoder <br /> |

## Public methods

### append

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

```
public Writer append (CharSequence csq, 
                int start, 
                int end)
```

Appends a subsequence of the specified character sequence to this writer. `Appendable`.

An invocation of this method of the form `out.append(csq, start, end)` when `csq` is not `null` behaves in exactly the same way as the invocation

    out.write(csq.subSequence(start, end).toString())

<br />

<br />

| Parameters ||
|---|---|
| `csq` | `CharSequence`: The character sequence from which a subsequence will be appended. If `csq` is `null`, then characters will be appended as if `csq` contained the four characters `"null"`. <br /> |
| `start` | `int`: The index of the first character in the subsequence <br /> |
| `end` | `int`: The index of the character following the last character in the subsequence <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Writer` | This writer <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### append

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

```
public Writer append (CharSequence csq)
```

Appends the specified character sequence to this writer.

An invocation of this method of the form `out.append(csq)` behaves in exactly the same way as the invocation

```
    out.write(csq.toString()) 
```

Depending on the specification of `toString` for the character sequence `csq`, the entire sequence may not be appended. For instance, invoking the `toString` method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

<br />

| Parameters ||
|---|---|
| `csq` | `CharSequence`: The character sequence to append. If `csq` is `null`, then the four characters `"null"` are appended to this writer. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Writer` | This writer <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### 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 ||
|---|---|
| `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 ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |

### getEncoding

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

```
public String getEncoding ()
```

Returns the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the `https://developer.android.com/reference/java/io/OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,%20java.lang.String)` constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return `null` if the stream has been closed.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | The historical name of this encoding, or possibly `null` if the stream has been closed <br /> |

**See also:**

- `https://developer.android.com/reference/java/nio/charset/Charset`

### 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 ||
|---|---|
| `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 str, 
                int off, 
                int len)
```

Writes a portion of a string.

<br />

| Parameters ||
|---|---|
| `str` | `String`: A String <br /> |
| `off` | `int`: Offset from which to start writing characters <br /> |
| `len` | `int`: Number of characters to write <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `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 string |

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

<br />

| Parameters ||
|---|---|
| `cbuf` | `char`: Buffer of characters <br /> |
| `off` | `int`: Offset from which to start writing characters <br /> |
| `len` | `int`: Number of characters to write <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an I/O error occurs |
| `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 |