# CharArrayWriter

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

# CharArrayWriter

`
public


class
CharArrayWriter
`  
`


``

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

<br />

*** ** * ** ***

This class implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().

Note: Invoking close() on this class has no effect, and methods of this class can be called after the stream has closed without generating an IOException.

## Summary

|                                                               ### Fields                                                               ||
|---------------------|-------------------------------------------------------------------------------------------------------------------|
| ` protected char[]` | [buf](https://developer.android.com/reference/java/io/CharArrayWriter#buf) The buffer where data is stored.       |
| ` protected int`    | [count](https://developer.android.com/reference/java/io/CharArrayWriter#count) The number of chars in the buffer. |

|                                                                                                                                                                                                                                                    ### 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                                                                                      ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter#CharArrayWriter())`() ` Creates a new CharArrayWriter.                                                   |
| ` `[CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter#CharArrayWriter(int))`(int initialSize) ` Creates a new CharArrayWriter with the specified initial size. |

|                                                                                                                                                                                           ### Public methods                                                                                                                                                                                            ||
|---------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | ` `[append](https://developer.android.com/reference/java/io/CharArrayWriter#append(char))`(char c) ` Appends the specified character to this writer.                                                                                                                                                             |
| ` `[CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | ` `[append](https://developer.android.com/reference/java/io/CharArrayWriter#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. |
| ` `[CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | ` `[append](https://developer.android.com/reference/java/io/CharArrayWriter#append(java.lang.CharSequence))`(`[CharSequence](https://developer.android.com/reference/java/lang/CharSequence)` csq) ` Appends the specified character sequence to this writer.                                                    |
| ` void`                                                                               | ` `[close](https://developer.android.com/reference/java/io/CharArrayWriter#close())`() ` Close the stream.                                                                                                                                                                                                       |
| ` void`                                                                               | ` `[flush](https://developer.android.com/reference/java/io/CharArrayWriter#flush())`() ` Flush the stream.                                                                                                                                                                                                       |
| ` void`                                                                               | ` `[reset](https://developer.android.com/reference/java/io/CharArrayWriter#reset())`() ` Resets the buffer so that you can use it again without throwing away the already allocated buffer.                                                                                                                      |
| ` int`                                                                                | ` `[size](https://developer.android.com/reference/java/io/CharArrayWriter#size())`() ` Returns the current size of the buffer.                                                                                                                                                                                   |
| ` char[]`                                                                             | ` `[toCharArray](https://developer.android.com/reference/java/io/CharArrayWriter#toCharArray())`() ` Returns a copy of the input data.                                                                                                                                                                           |
| ` `[String](https://developer.android.com/reference/java/lang/String)                 | ` `[toString](https://developer.android.com/reference/java/io/CharArrayWriter#toString())`() ` Converts input data to a string.                                                                                                                                                                                  |
| ` void`                                                                               | ` `[write](https://developer.android.com/reference/java/io/CharArrayWriter#write(int))`(int c) ` Writes a character to the buffer.                                                                                                                                                                               |
| ` void`                                                                               | ` `[write](https://developer.android.com/reference/java/io/CharArrayWriter#write(java.lang.String,%20int,%20int))`(`[String](https://developer.android.com/reference/java/lang/String)` str, int off, int len) ` Write a portion of a string to the buffer.                                                      |
| ` void`                                                                               | ` `[write](https://developer.android.com/reference/java/io/CharArrayWriter#write(char[],%20int,%20int))`(char[] c, int off, int len) ` Writes characters to the buffer.                                                                                                                                          |
| ` void`                                                                               | ` `[writeTo](https://developer.android.com/reference/java/io/CharArrayWriter#writeTo(java.io.Writer))`(`[Writer](https://developer.android.com/reference/java/io/Writer)` out) ` Writes the contents of the buffer to another character stream.                                                                  |

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

## Fields

### buf

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

```
protected char[] buf
```

The buffer where data is stored.

<br />

### count

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

```
protected int count
```

The number of chars in the buffer.

<br />

## Public constructors

### CharArrayWriter

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

```
public CharArrayWriter ()
```

Creates a new CharArrayWriter.

<br />

### CharArrayWriter

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

```
public CharArrayWriter (int initialSize)
```

Creates a new CharArrayWriter with the specified initial size.

<br />

|                               Parameters                                ||
|---------------|----------------------------------------------------------|
| `initialSize` | `int`: an int specifying the initial buffer size. <br /> |

|                                                               Throws                                                               ||
|--------------------------------------------------------------------------------------------------------|----------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if initialSize is negative |

## Public methods

### append

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

```
public CharArrayWriter 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                                                 ||
|------------------------------------------------------------------------------------|--------------------|
| [CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | This writer <br /> |

### append

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

```
public CharArrayWriter 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                                                 ||
|------------------------------------------------------------------------------------|--------------------|
| [CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | This writer <br /> |

|                                                                                                  Throws                                                                                                  ||
|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| [IndexOutOfBoundsException](https://developer.android.com/reference/java/lang/IndexOutOfBoundsException) | If`start`or`end`are negative,`start`is greater than`end`, or`end`is greater than`csq.length()` |

### append

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

```
public CharArrayWriter 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                                                 ||
|------------------------------------------------------------------------------------|--------------------|
| [CharArrayWriter](https://developer.android.com/reference/java/io/CharArrayWriter) | This writer <br /> |

### close

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

```
public void close ()
```

Close the stream. This method does not release the buffer, since its contents might still be required. Note: Invoking this method in this class will have no effect.

<br />

### flush

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

```
public void flush ()
```

Flush the stream.

<br />

### reset

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

```
public void reset ()
```

Resets the buffer so that you can use it again without throwing away the already allocated buffer.

<br />

### size

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

```
public int size ()
```

Returns the current size of the buffer.

<br />

|                              Returns                              ||
|-------|------------------------------------------------------------|
| `int` | an int representing the current size of the buffer. <br /> |

### toCharArray

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

```
public char[] toCharArray ()
```

Returns a copy of the input data.

<br />

|                            Returns                             ||
|----------|------------------------------------------------------|
| `char[]` | an array of chars copied from the input data. <br /> |

### toString

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

```
public String toString ()
```

Converts input data to a string.

<br />

|                                        Returns                                         ||
|--------------------------------------------------------------------|--------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the string. <br /> |

### write

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

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

Writes a character to the buffer.

<br />

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

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

Write a portion of a string to the buffer.

<br />

|                             Parameters                             ||
|-------|-------------------------------------------------------------|
| `str` | `String`: String to be written from <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`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[] c, 
                int off, 
                int len)
```

Writes characters to the buffer.

<br />

|                        Parameters                         ||
|-------|----------------------------------------------------|
| `c`   | `char`: the data to be written <br />              |
| `off` | `int`: the start offset in the data <br />         |
| `len` | `int`: the number of chars that are written <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 |

### writeTo

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

```
public void writeTo (Writer out)
```

Writes the contents of the buffer to another character stream.

<br />

|                      Parameters                       ||
|-------|------------------------------------------------|
| `out` | `Writer`: the output stream to write to <br /> |

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