> Documentation index: https://developer.android.com/reference/llms.txt

Summary: [Inherited Fields](https://developer.android.com/reference/java/io/StringWriter.md.txt#inhfields) \| [Ctors](https://developer.android.com/reference/java/io/StringWriter.md.txt#pubctors) \| [Methods](https://developer.android.com/reference/java/io/StringWriter.md.txt#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/io/StringWriter.md.txt#inhmethods)

# StringWriter


`
public


class
StringWriter
`


`


`

`

extends Writer


`

`


`

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

<br />

*** ** * ** ***

A character stream that collects its output in a string buffer, which can
then be used to construct a string.


Closing a `StringWriter` has no effect. The methods in this class
can be called after the stream has been closed without generating an
`IOException`.

## Summary

### Inherited fields

From class `java.io.Writer` `
protected


Object` `lock`

The object used to synchronize operations on this stream.


| ### Public constructors ||
|---|---|
| ` StringWriter() ` Create a new string writer using the default initial string-buffer size. |
| ` StringWriter(int initialSize) ` Create a new string writer using the specified initial string-buffer size. |

| ### Public methods ||
|---|---|
| ` StringWriter` | ` append(char c) ` Appends the specified character to this writer. |
| ` StringWriter` | ` append(CharSequence csq, int start, int end) ` Appends a subsequence of the specified character sequence to this writer. |
| ` StringWriter` | ` append(CharSequence csq) ` Appends the specified character sequence to this writer. |
| ` void` | ` close() ` Closing a `StringWriter` has no effect. |
| ` void` | ` flush() ` Flush the stream. |
| ` StringBuffer` | ` getBuffer() ` Return the string buffer itself. |
| ` String` | ` toString() ` Return the buffer's current value as a string. |
| ` void` | ` write(String str) ` Write a string. |
| ` void` | ` write(int c) ` Write a single character. |
| ` void` | ` write(String str, int off, int len) ` Write a portion of a string. |
| ` void` | ` write(char[] cbuf, int off, int len) ` Write a portion of an array of characters. |

### Inherited methods

From class `

java.io.Writer

` `


Writer` `
append(char c)
`

Appends the specified character to this writer.


`


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

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


`


Writer` `
append(CharSequence csq)
`

Appends the specified character sequence to this writer.


`
abstract


void` `
close()
`

Closes the stream, flushing it first.


`
abstract


void` `
flush()
`

Flushes the stream.


`


static


Writer` `
nullWriter()
`

Returns a new `Writer` which discards all characters.


`


void` `
write(String str)
`

Writes a string.


`


void` `
write(int c)
`

Writes a single character.


`


void` `
write(String str, int off, int len)
`

Writes a portion of a string.


`
abstract


void` `
write(char[] cbuf, int off, int len)
`

Writes a portion of an array of characters.


`


void` `
write(char[] cbuf)
`

Writes an array of characters.


From class `

java.lang.Object

` `


Object` `
clone()
`

Creates and returns a copy of this object.


`


boolean` `
equals(Object obj)
`

Indicates whether some other object is "equal to" this one.


`


void` `
finalize()
`

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.


`


final

Class<?>` `
getClass()
`

Returns the runtime class of this `Object`.


`


int` `
hashCode()
`

Returns a hash code value for the object.


`


final

void` `
notify()
`

Wakes up a single thread that is waiting on this object's
monitor.


`


final

void` `
notifyAll()
`

Wakes up all threads that are waiting on this object's monitor.


`


String` `
toString()
`

Returns a string representation of the object.


`


final

void` `
wait(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(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()
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*.


From interface `

java.lang.Appendable

` `
abstract


Appendable` `
append(char c)
`

Appends the specified character to this `Appendable`.


`
abstract


Appendable` `
append(CharSequence csq, int start, int end)
`

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


`
abstract


Appendable` `
append(CharSequence csq)
`

Appends the specified character sequence to this `Appendable`.


From interface `

java.io.Closeable

` `
abstract


void` `
close()
`

Closes this stream and releases any system resources associated
with it.


From interface `

java.io.Flushable

` `
abstract


void` `
flush()
`

Flushes this stream by writing any buffered output to the underlying
stream.


From interface `

java.lang.AutoCloseable

` `
abstract


void` `
close()
`

Closes this resource, relinquishing any underlying resources.


## Public constructors

### StringWriter

```
public StringWriter ()
```

Create a new string writer using the default initial string-buffer
size.

<br />

### StringWriter

```
public StringWriter (int initialSize)
```

Create a new string writer using the specified initial string-buffer
size.

<br />

| Parameters ||
|---|---|
| `initialSize` | `int`: The number of `char` values that will fit into this buffer before it is automatically expanded <br /> |

| Throws ||
|---|---|
| `IllegalArgumentException` | If `initialSize` is negative |

## Public methods

### append

```
public StringWriter append (char c)
```

Appends the specified character to this writer.

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

```
    out.write(c) 
```

<br />

<br />

| Parameters ||
|---|---|
| `c` | `char`: The 16-bit character to append <br /> |

| Returns ||
|---|---|
| `StringWriter` | This writer <br /> |

### append

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

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

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 ||
|---|---|
| `StringWriter` | This writer <br /> |

| Throws ||
|---|---|
| `IndexOutOfBoundsException` | If `start` or `end` are negative, `start` is greater than `end`, or `end` is greater than `csq.length()` |

### append

```
public StringWriter 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 ||
|---|---|
| `StringWriter` | This writer <br /> |

### close

```
public void close ()
```

Closing a `StringWriter` has no effect. The methods in this
class can be called after the stream has been closed without generating
an `IOException`.

<br />

| Throws ||
|---|---|
| `IOException` |   |

### flush

```
public void flush ()
```

Flush the stream.

The `flush` method of `StringWriter` does nothing.

<br />

### getBuffer

```
public StringBuffer getBuffer ()
```

Return the string buffer itself.

<br />

| Returns ||
|---|---|
| `StringBuffer` | StringBuffer holding the current buffer value. <br /> |

### toString

```
public String toString ()
```

Return the buffer's current value as a string.

<br />

| Returns ||
|---|---|
| `String` | a string representation of the object. <br /> |

### write

```
public void write (String str)
```

Write a string.

<br />

| Parameters ||
|---|---|
| `str` | `String`: String to be written <br /> |

### write

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

Write a single character.

<br />

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

### write

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

Write a portion of a string.

<br />

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

| Throws ||
|---|---|
| `IndexOutOfBoundsException` | If `off` is negative, or `len` is negative, or `off + len` is negative or greater than the length of the given string |

### write

```
public void write (char[] cbuf, 
                int off, 
                int len)
```

Write a portion of an array of characters.

<br />

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

| Throws ||
|---|---|
| `IndexOutOfBoundsException` | If `off` is negative, or `len` is negative, or `off + len` is negative or greater than the length of the given array |