< Prev
Next >


CharArrayWriter class is a subclass of Writer abstract class and it is used to write the contents of a char array to its own buffer(also a char array) and from this buffer, characters can be written to a character stream.

Constructor :

This constructor creates a CharArrayWriter object for writing out the contents of a char array.
char[] arr1= new char[]{'H','e','l','l','o'}; 

CharArrayWriter caw= new CharArrayWriter();
for(char ch : arr) 				//Using for-each loop to write one char at a time
We are going to write characters in char array, arr, to the internal buffer of CharArrayWriter, from this buffer, characters can be written to a character stream.

Methods for writing using CharArrayWriter

Some methods that we generally use while working with CharArrayWriter are shown in the table below :

Methods Description
This method flushes any data to be written out of this character output stream's buffer.
write(int c)
This method writes one character at a time to this character output stream.

This method writes a whole character array at a time to this character output stream.

write(String str)
This method writes a String to this character output stream
writeTo(Writer outstream)
This method writes the contents of the buffer to a different character stream.

This method closes this output stream and also frees any resources connected with this output stream.

Writing a char/char array to a file using CharArrayWriter class.

We are going to initialize the buffer of CharArrayWriter to the value of two char arrays(one by one) and then writing the content of this buffer to a file - TextBook2.txt using its path D:\\TextBook.txt.
//Program to use a character buffer that can be used as character input stream

import java.io.*;

class A
public static void main(String... ar)
char[] arr1= new char[]{'H','e','l','l','o'};
char[] arr2= new char[]{'W','o','r','l','d'};

CharArrayWriter caw= new CharArrayWriter(); // a character buffer is created, 
					    //big enough to hold character array

System.out.println("Initial Size of the buffer = "+ caw.size());

//Using for-each loop to write one char a time, to buffer
for(char ch : arr1) 

//Writing whole character array at once, to buffer

System.out.println("Content of buffer = "+ caw.toString());
System.out.println("Size of the buffer = "+ caw.size());

//Writing the content of buffer of CharArrayWriter to a file
FileWriter fw = new FileWriter("D:\\TextBook2.txt");

catch(IOException e)


Output is :

Initial Size of the buffer = 0
Content of buffer = HelloWorld
Size of the buffer = 10

Program Analysis


Even after closing the Writer by calling close(), we can still write the characters to the buffer of CharArrayWriter and IOException will be thrown.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
< CharacterArrayReader

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page