Advertisement



< Prev
Next >



CharArrayWriter



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 :


CharArrayWriter()
This constructor creates a CharArrayWriter object for writing out the contents of a char array.
Example-:
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
caw.write(ch);
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
flush()
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.

write(char[])
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.

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




Advertisement




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'};

try
{
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) 
{
caw.write(ch);
}

//Writing whole character array at once, to buffer
caw.write(arr2);  

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");
caw.writeTo(fw);

fw.flush();
caw.flush();
caw.close();
}		 	 
catch(IOException e)
{
System.out.println(e);
}

}
}


Output is :


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


Program Analysis







Note

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 -





< Prev
< CharacterArrayReader
FileReader>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement