Java CharArrayWriter ClassLast Updated : 27 Jan 2026 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. What is CharArrayWriter Class in Java?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. CharArrayWriter Class DeclarationThe following declaration shows how the CharArrayWriter class is defined: Constructors of CharArrayWriter ClassThe CharArrayWriter class provides constructors to create an in-memory character output stream. 1. CharArrayWriter()This constructor creates a new character array writer with an initial default buffer size. Syntax: Here is the syntax: 2. CharArrayWriter(int size)This constructor creates a new character array writer with a specified initial buffer size. Syntax: Here is the syntax: Example of CharArrayWriter Class ConstructorsThe following code shows how the constructors of the CharArrayWriter class are used. Methods of CharArrayWriter ClassThe CharArrayWriter class provides methods to write and manage character data in memory. The following table lists important methods of CharArrayWriter class.
Examples of Java CharArrayWriter ClassThe following examples demonstrate how to use the Java CharArrayWriter class. Example 1: Writing Common Data to Multiple FilesThe 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 Example 2: Converting CharArrayWriter Data to StringThe following example demonstrates how to convert the buffer data of CharArrayWriter into a string. Output: Java CharArrayWriter Example Next TopicJava PrintStream Class |
We request you to subscribe our newsletter for upcoming updates.