Open In App

Java CharArrayWriter Class | Set 1

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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();
    }
}

Output
Geeks


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();
    }
}

Output
One


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();
    }
}

Output
Java


Java CharArrayWriter Methods

The image below demonstrates the methods of CharArrayWriter class.

CharArrayWriter class in Java - Set 1


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



Next Article
Article Tags :
Practice Tags :

Similar Reads