The CharArrayWriter class in Java is used to write character data into an in-memory buffer that can be reused to write data to multiple output streams.
In this chapter, we will learn what the CharArrayWriter class is, why it is used, its declaration, constructors, important methods, and how to write common data to multiple files using examples.
The CharArrayWriter class is a character-based output stream that writes data into an internal character buffer instead of writing directly to a file.
It extends the Writer class and is mainly used when the same character data needs to be written to multiple destinations. The internal buffer grows automatically as data is written, and calling the close() method has no effect on the stream.
The following declaration shows how the CharArrayWriter class is defined:
The CharArrayWriter class provides constructors to create an in-memory character output stream.
This constructor creates a new character array writer with an initial default buffer size.
Syntax:
Here is the syntax:
This constructor creates a new character array writer with a specified initial buffer size.
Syntax:
Here is the syntax:
The following code shows how the constructors of the CharArrayWriter class are used.
The CharArrayWriter class provides methods to write and manage character data in memory. The following table lists important methods of CharArrayWriter class.
| Method | Description |
|---|---|
| int size() | It is used to return the current size of the buffer. |
| char[] toCharArray() | It is used to return the copy of an input data. |
| String toString() | It is used for converting an input data to a string. |
| CharArrayWriter append(char c) | It is used to append the specified character to the writer. |
| CharArrayWriter append(CharSequence csq) | It is used to append the specified character sequence to the writer. |
| CharArrayWriter append(CharSequence csq, int start, int end) | It is used to append the subsequence of a specified character to the writer. |
| void write(int c) | It is used to write a character to the buffer. |
| void write(char[] c, int off, int len) | It is used to write a character to the buffer. |
| void write(String str, int off, int len) | It is used to write a portion of string to the buffer. |
| void writeTo(Writer out) | It is used to write the content of buffer to different character stream. |
| void flush() | It is used to flush the stream. |
| void reset() | It is used to reset the buffer. |
| void close() | It is used to close the stream. |
The following examples demonstrate how to use the Java CharArrayWriter class.
The following example demonstrates how to write the same character data to multiple files using the CharArrayWriter class.
Output:
Success...
File Content (a.txt, b.txt, c.txt, d.txt):
Welcome to TpointTech
The following example demonstrates how to convert the buffer data of CharArrayWriter into a string.
Output:
Java CharArrayWriter Example
We request you to subscribe our newsletter for upcoming updates.