Wednesday, February 1, 2012

Transfering the content of CharArrayWriter to FileWriter


// Usage of CharArrayWriter (It uses character array as a destination)


import java.io.*;
class CharArrayWriterDemo
{
public static void main(String args[]) throws IOException
{
CharArrayWriter f = new CharArrayWriter();
String s = "Welcome to CharArrayWriter";
char buf[] = new char[s.length()];
s.getChars(0, s.length(), buf, 0);
f.write(buf);

//f.reset();

System.out.println("Buffer as a string");
System.out.println(f.toString());

System.out.println("Into array");
char c[] = f.toCharArray();
for (int i=0; i<c.length; i++)
{
System.out.print(c[i]);
}

System.out.println("\nTo a FileWriter()");
FileWriter f2 = new FileWriter("test.txt");
f.writeTo(f2);
f2.close();

System.out.println("Doing a reset");
f.reset();

for (int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());
}
}

Output:



Buffer as a string
Welcome to CharArrayWriter
Into array
Welcome to CharArrayWriter
To a FileWriter()
==>here text.txt file is created.
Doing a reset
XXX


No comments:

Post a Comment