CharArrayWriter implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().
Note: Invoking close() on this class has no effect, and methods of this class can be called after the stream has closed without generating an IOException.
Constructors
Methods
package com.java.io;
import java.io.CharArrayWriter;
import java.io.IOException;
public class CharArrayWriterExample {
public static void main(String[] args) {
CharArrayWriter charArrayWriter = null;
try {
charArrayWriter = new CharArrayWriter();
charArrayWriter.write(" data1 ");
charArrayWriter.write(" data2 ");
charArrayWriter.write(" data3 ");
char[] chars = charArrayWriter.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Closing the streams
if (charArrayWriter != null)
charArrayWriter.close();
}
}
}
data1 data2 data3
Related Article