Java CharArrayWriter Class | Set 1
Last Updated :
18 Apr, 2025
The CharArrayWriter class in Java is part of the java.io package, and it is used to write data into a character array. This class is used when we want to store characters temporarily without dealing with files and other storage.
Features of CharArrayWriter Class:,
- It is used to store characters in memory, not in files.
- The internal buffer increases automatically as more characters are written to it.
- We can also convert the stored characters into a String[] or a char[] array.
- It is useful when we want to store the characters temporarily in memory.
Example:
Java
// Demonstrating the working of CharArrayWriter
import java.io.CharArrayWriter;
public class Geeks {
public static void main(String[] args) {
// Create a CharArrayWriter
CharArrayWriter w = new CharArrayWriter();
// Write some characters
w.write('G');
w.write('e');
w.write('e');
w.write('k');
w.write('s');
// Convert it to a string and print it
String s = w.toString();
System.out.println(s);
// Close the writer
w.close();
}
}
Declaration of CharArrayWriter Class
The Declaration of CharArrayWriter class is listed below:
public class CharArrayWriter extends Writer
All Implemented Interfaces:
- Closeable: This interface make sure that all the resources are properly released when we are done using the CharArrayWriter.
- Flushable: This inteface flush the content and making sure any stored data is written out.
- Appendable: This interface lets us add characters to the writer.
- AutoCloseable: This interface enables automatic resource management.
Constructors in CharArrayWriter in Java
This class consists of two constructors with the help of which we can create object of this class in different ways. The following are the constructors available in this class:
1. CharArrayWriter(): This constructor creates a new CharArrayWriter with an initial buffer size of 32 characters.
Syntax:
CharArrayWriter charArrayWriter = new CharArrayWriter();
Example:
Java
// Demonstrating the working of
// CharArrayWriter()
import java.io.CharArrayWriter;
public class Geeks {
public static void main(String[] args) {
// Create a CharArrayWriter with default size
CharArrayWriter w = new CharArrayWriter();
// Write some characters
w.write('O');
w.write('n');
w.write('e');
// Convert it to a string and print it
String s = w.toString();
System.out.println(s);
// Close the writer
w.close();
}
}
2. CharArrayWriter(int size): This constructor creates a new CharArrayWriter with the specified initial buffer size.
Syntax:
CharArrayWriter charArrayWriter = new CharArrayWriter(64);
Example:
Java
// Demonstrating the working
// of CharArrayWriter(int size)
import java.io.CharArrayWriter;
public class Geeks {
public static void main(String[] args) {
// Create a CharArrayWriter with a specified initial size
CharArrayWriter w = new CharArrayWriter(50); // Size is 50
// Write some characters
w.write('J');
w.write('a');
w.write('v');
w.write('a');
// Convert it to a string and print it
String s = w.toString();
System.out.println(s);
// Close the writer
w.close();
}
}
Java CharArrayWriter Methods
The image below demonstrates the methods of CharArrayWriter class.

Now, we are going to discuss about each method one by one in detail:
1. write(int char): This method is used to writes a single character to the writer.
Syntax:
public void write(int char)
Parameters: The integer value of the character to be written.
2. write(String str, int offset, int maxlen): This method is used to writes a part of a string to the writer.
Syntax:
public void write(String str, int offset, int maxlen)
Parameters: This method includes three parameters which are listed below
- str: The string to be written to the Writer.
- offset: It is the starting position of the string.
- maxlen: Maximum number of characters to write from the string.
3. write(char[] carray, int offset, int maxlen): This method is used to writes a part of a character array to the Writer. We can tell where to start in the array and how many characters to write.
Syntax:
public void write(char[] carray, int offset, int maxlen)
Parameters: This method includes three parameters which are listed below
- carray: The character array to be written to the Writer.
- offset: It is the starting position in the character array
- maxlen: Maximum number of characters to write from the character array.
4. writeTo(Writer out_stream): This method is used to Writes the content of the buffer to another specified stream.
Syntax:
public void writeTo(Writer out_stream)
Parameters: The destination Writer stream to which the buffer content will be written.
5. toString(): This method is used to convert the buffer content into a string representation.
Syntax:
public String toString()
- Parameters: This method does not take any parameter
- Return Type: This method returns the buffer content as a string
6. close(): This method closes the writer stream but does not release the buffer.
- Parameters: This method does not take any parameter.
7. size(): This method returns the size of a buffer.
- Parameters: This method does not take any parameter.
Java Program to Demonstrate Methods of CharArrayWriter Class
Example:
Java
// Java program illustrating the working of CharArrayWriter class methods
// write(int char), toString(), write(char[] carray, int offset, int maxlen)
// write(String str, int offset, int maxlen), size()
import java.io.*;
public class Geeks {
public static void main(String[] args) throws IOException
{
// Initializing the character array
char[] geek = {'G', 'E', 'E', 'K', 'S'};
String geek_str;
// Initializing the CharArrayWriter
CharArrayWriter char_array1 = new CharArrayWriter();
CharArrayWriter char_array2 = new CharArrayWriter();
CharArrayWriter char_array3 = new CharArrayWriter();
for(int c = 72; c < 77; c++)
{
// Use of write(int char)
// Writer int value to the Writer
char_array1.write(c);
}
// Use of toString() : returning Buffer content as String
geek_str = char_array1.toString();
System.out.println("Using write(int char) : "+ geek_str);
// Use of write(String str, int offset, int maxlen)
// writes some part of the string to the Writer.
char_array2.write(geek_str, 2, 3);
System.out.println("write(str, offset, maxlen) : "+ char_array2.toString());
// Use of write(char[] carray, int offset, int maxlen)
// writes some part of the Char[] geek to the Writer
char_array3.write(geek, 2, 3);
System.out.println("write(carray, offset, maxlen) : "+ char_array3.toString());
// get buffered content as string
String str = char_array3.toString();
// Use of writeTo(Writer out_stream)
char_array3.writeTo(char_array1);
System.out.println("\nchar_array3 to char_array1 : "+ char_array1.toString());
// Use of size() method
System.out.println("\nSize of char_array1 : "+ char_array1.size());
System.out.println("Size of char_array1 : "+ char_array2.size());
System.out.println("Size of char_array1 : "+ char_array3.size());
}
}
Output:
Using write(int char) : HIJKL
write(str, offset, maxlen) : JKL
write(carray, offset, maxlen) : EKS
char_array3 to char_array1 : HIJKLEKS
Size of char_array1 : 8
Size of char_array1 : 3
Size of char_array1 : 3
To know about other methods, refer this article: Java.io.CharArrayWriter class in Java | Set2
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read