Character Array Readers and Writers
java.io.ByteArrayOutputStream classes let
programmers use stream methods to read and write arrays of bytes. The
java.io.CharArrayWriter classes allow programmers
methods to read and write arrays of
char arrays are purely internal to Java and thus
composed of true Unicode characters, this is one of the few uses of
readers and writers where you don’t need to concern yourself
with conversions between different encodings. If you want to read
arrays of text encoded in some non-Unicode encoding, you should chain
ByteArrayInputStream to an
InputStreamReader instead. Similarly, to write
text into a byte array in a non-Unicode encoding, just chain an
OutputStreamWriter to a
The CharArrayWriter Class
CharArrayWriter maintains an internal array of
chars into which successive characters are
written. The array is expanded as needed. This array is stored in a
protected field called
protected char buf
For efficiency, the array generally contains more components than
characters. The number of characters actually written is stored in a
int field called
protected int count
The value of the
count field is always less than
or equal to
The no-argument constructor creates a
CharArrayWriter object with a 32-character buffer.
This is on the small side, so you can expand it with the second
public CharArrayWriter() ...
Get Java I/O now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.