Advertisement
CharArrayWriter() |
---|
This constructor creates a CharArrayWriter object for writing out the contents of a char array. Example-:
|
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
//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);
}
}
}
Initial Size of the buffer = 0
Content of buffer = HelloWorld
Size of the buffer = 10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement