CharArrayWriter Class In Java
CharArrayWriter Class
In Java, when we want to write the data into the multiple files that time CharArrayWriter class can be used. This class implements the Appendable interface and its buffer automatically grows, when the data is written in it.
Let’s see an example, given below.
Code
- import java.io.*;
- public class FileHandlingExample {
- public static void main(String args[]) throws Exception {
- CharArrayWriter out = new CharArrayWriter();
- out.write("My language is Java");
- FileWriter f1 = new FileWriter("Simple1.txt");
- FileWriter f2 = new FileWriter("Simple2.txt");
- out.writeTo(f1);
- out.writeTo(f2);
- f1.close();
- f2.close();
- }
- }
In the example, mentioned above, we write a data to 4 files Simple1.txt, Simple2.txt, Simple3.txt and Simple4.txt.
Summary
Thus, we learnt that when we want to write the data into multiple files that time CharArrayWriter class can be used in Java and also learnt how to create it.