Java CharArrayWriter Class
Last modified: April 16, 2025
The java.io.CharArrayWriter
class implements a character buffer that
can be used as a Writer. The buffer grows automatically as data is written to it.
Data can be retrieved using toCharArray() and toString() methods.
CharArrayWriter
is useful when you need to write characters to memory
rather than a file or network. It provides methods to access the written data as
a character array or string. The class handles all character encoding internally.
CharArrayWriter Class Overview
CharArrayWriter
extends Writer
and provides in-memory
character buffer operations. Key methods include write operations, buffer size
management, and data retrieval. The buffer automatically expands as needed.
public class CharArrayWriter extends Writer { public CharArrayWriter(); public CharArrayWriter(int initialSize); public void write(int c); public void write(char[] c, int off, int len); public void write(String str, int off, int len); public void writeTo(Writer out); public void append(CharSequence csq); public void append(CharSequence csq, int start, int end); public void append(char c); public void reset(); public char[] toCharArray(); public int size(); public String toString(); public void flush(); public void close(); }
The code above shows key methods provided by CharArrayWriter
. These
methods allow writing characters to an in-memory buffer and retrieving them. The
class is particularly useful for testing or when you need to capture output.
Creating a CharArrayWriter
CharArrayWriter can be created with a default initial size or a custom size. The buffer grows automatically when needed. The default constructor creates a buffer with 32 characters initial capacity.
import java.io.CharArrayWriter; public class Main { public static void main(String[] args) { // Create with default size CharArrayWriter writer1 = new CharArrayWriter(); // Create with custom initial size CharArrayWriter writer2 = new CharArrayWriter(1024); System.out.println("Default size writer created"); System.out.println("Custom size (1024) writer created"); // Write some data writer1.write("Hello"); writer2.write("World"); System.out.println("Writer1 content: " + writer1.toString()); System.out.println("Writer2 content: " + writer2.toString()); writer1.close(); writer2.close(); } }
This example demonstrates different ways to create CharArrayWriter. The first uses default size while the second specifies 1024 characters. Data is written to both writers and then retrieved as strings. Always close writers when done.
Writing Data to CharArrayWriter
CharArrayWriter provides multiple write methods for different data types. You can write single characters, character arrays, strings, or portions of them. All data is appended to the internal buffer.
import java.io.CharArrayWriter; public class Main { public static void main(String[] args) { try (CharArrayWriter writer = new CharArrayWriter()) { // Write single character writer.write('A'); // Write character array char[] chars = {'B', 'C', 'D'}; writer.write(chars); // Write portion of character array writer.write(chars, 1, 2); // Write string writer.write("EFG"); // Write portion of string writer.write("HIJKLM", 2, 3); System.out.println("Writer content: " + writer.toString()); } } }
This example shows various write operations supported by CharArrayWriter. The output combines all written data: 'A' + "BCD" + "CD" + "EFG" + "JKL". The try-with-resources ensures proper cleanup. The buffer automatically grows as needed.
Retrieving Data from CharArrayWriter
Data can be retrieved from CharArrayWriter as a character array or string. The toCharArray() method returns a copy of the buffer contents. The toString() method converts the buffer to a string.
import java.io.CharArrayWriter; public class Main { public static void main(String[] args) { CharArrayWriter writer = new CharArrayWriter(); writer.write("Lorem ipsum dolor sit amet"); // Get as character array char[] charArray = writer.toCharArray(); System.out.println("Character array length: " + charArray.length); System.out.println("First character: " + charArray[0]); // Get as string String content = writer.toString(); System.out.println("String content: " + content); // Get size System.out.println("Buffer size: " + writer.size()); writer.close(); } }
This example demonstrates retrieving data from CharArrayWriter. The toCharArray() method returns a new character array copy. The toString() method creates a string from the buffer. The size() method returns the current number of characters in the buffer.
Writing to Another Writer
The writeTo() method writes the buffer contents to another Writer. This is useful for transferring collected data to another output destination. The method writes the entire buffer contents.
import java.io.CharArrayWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { CharArrayWriter writer = new CharArrayWriter(); writer.write("This text will be written to a file"); try (FileWriter fileWriter = new FileWriter("output.txt")) { // Write buffer contents to file writer.writeTo(fileWriter); System.out.println("Data written to file successfully"); } catch (IOException e) { e.printStackTrace(); } writer.close(); } }
This example shows how to transfer data from CharArrayWriter to a FileWriter. The writeTo() method writes all collected characters to the specified Writer. This pattern is useful when you need to buffer output before writing it to its final destination.
Resetting the Buffer
The reset() method clears the buffer contents while keeping the underlying buffer allocated. This is more efficient than creating a new CharArrayWriter when you need to reuse the buffer. The buffer capacity remains unchanged.
import java.io.CharArrayWriter; public class Main { public static void main(String[] args) { CharArrayWriter writer = new CharArrayWriter(); // First usage writer.write("First content"); System.out.println("First content: " + writer.toString()); System.out.println("Size after first write: " + writer.size()); // Reset buffer writer.reset(); System.out.println("Size after reset: " + writer.size()); // Second usage writer.write("New content"); System.out.println("Second content: " + writer.toString()); System.out.println("Size after second write: " + writer.size()); writer.close(); } }
This example demonstrates using reset() to reuse a CharArrayWriter. After writing data and calling reset(), the buffer is empty but maintains its capacity. This is more efficient than creating new instances when processing multiple items.
Appending Data
CharArrayWriter implements Appendable, allowing data to be appended using the append() methods. These methods work similarly to write() but return the CharArrayWriter for method chaining. They accept characters, CharSequences, or portions of CharSequences.
import java.io.CharArrayWriter; public class Main { public static void main(String[] args) { CharArrayWriter writer = new CharArrayWriter(); // Append various data types writer.append('A') .append("BC") .append("DEF", 1, 3) .append('G'); System.out.println("Appended content: " + writer.toString()); // Append with StringBuilder StringBuilder sb = new StringBuilder("HIJ"); writer.append(sb); System.out.println("After StringBuilder append: " + writer.toString()); writer.close(); } }
This example shows how to use append() methods for fluent writing. The methods return the CharArrayWriter instance, allowing method chaining. The output is "ABCDEFGHIJ". Append operations are convenient for building complex output through chained calls.
Source
Java CharArrayWriter Class Documentation
In this article, we've covered the essential methods and features of the Java CharArrayWriter class. Understanding these concepts is crucial for working with in-memory character buffers in Java applications.
Author
List all Java tutorials.