Java IO: CharArrayWriter

Jakob Jenkov
Last update: 2015-09-08

The Java CharArrayWriter class (java.io.CharArrayWriter) makes it possible to write characters via the Writer methods (CharArrayWriter is a subclass of Writer) and convert the written characters into a char array.

The Java CharArrayWriter class is handy when you have a component that can only write characters to a Writer, but you need the characters as a char array. Simply pass that component a CharArrayWriter and when all characters are written to it, call toCharArray() on the CharArrayWriter.

CharArrayWriter Example

Here is a simple Java CharArrayWriter example:

CharArrayWriter charArrayWriter = new CharArrayWriter();

charArrayWriter.write("CharArrayWriter");

char[] chars1 = charArrayWriter.toCharArray();

charArrayWriter.close();

This example first creates a Java CharArrayWriter. Second, the example writes a string to the CharArrayWriter. Third, the written characters are obtained as a char array by calling the toCharArray() method on the CharArrayWriter.

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

Setting Initial char Array Size

The Java CharArrayWriter has a constructor that lets you set the initial size of the char array used internally to store the written characters.

Setting the initial size does not prevent the CharArrayWriter from storing more characters than the initial size. If the number of characters written to the CharArrayWriter exceed the size of the initial char array, a new char array is created, and all characters are copied into the new array.

Here is how setting the initial char array size using the CharArrayWriter constructor looks:

int initialSize = 1024;

CharArrayWriter charArrayWriter =
    new CharArrayWriter(initialSize);

This example creates a CharArrayWriter with an initial char array size of 1024.

Closing a CharArrayWriter

When you are finished writing characters to a Java CharArrayWriter you can close. Since the CharArrayWriter is not connected to any underlying system resources, like files or network connections, it is not crucial to close it.

Closing a CharArrayWriter is done by calling its close() method. Here is how closing a Java CharArrayWriter looks:

charArrayWriter.close();

You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a CharArrayWriter looks with the try-with-resources construct:

try(CharArrayWriter charArrayWriter =
    new CharArrayWriter() ){

    charArrayWriter.write("data 1");
    charArrayWriter.write("data 2");
    charArrayWriter.write("data 3");

    char[] chars = charArrayWriter.toCharArray();
}

Notice how there is no longer any explicit close() method call to the CharArrayWriter instance. The try-with-resources construct takes care of that.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next