AdSense

AdSense3

Saturday 8 November 2014

CharArrayWriter class:

The CharArrayWriter class can be used to write data to multiple files. This class implements the Appendable interface. Its buffer automatically grows when data is written in this stream. Calling the close() method on this object has no effect.

Example of CharArrayWriter class:

In this example, we are writing a common data to 4 files a.txt, b.txt, c.txt and d.txt.
  1. import java.io.*;  
  2. class Simple{  
  3.  public static void main(String args[])throws Exception{  
  4.   
  5.   CharArrayWriter out=new CharArrayWriter();  
  6.   out.write("my name is");  
  7.   
  8.   FileWriter f1=new FileWriter("a.txt");  
  9.   FileWriter f2=new FileWriter("b.txt");  
  10.   FileWriter f3=new FileWriter("c.txt");  
  11.   FileWriter f4=new FileWriter("d.txt");  
  12.   
  13.   out.writeTo(f1);  
  14.   out.writeTo(f2);  
  15.   out.writeTo(f3);  
  16.   out.writeTo(f4);  
  17.   
  18.   
  19.   f1.close();  
  20.   f2.close();  
  21.   f3.close();  
  22.   f4.close();  
  23.  }  
  24. }  

No comments:

Post a Comment