582 Part II: The Java Library
CharArrayReader
CharArrayReaderis an implementation of an input stream that uses a character array as the
source. This class has two constructors, each of which requires a character array to provide
the data source:
CharArrayReader(chararray[ ])
CharArrayReader(chararray[ ], intstart, intnumChars)
Here,arrayis the input source. The second constructor creates aReaderfrom a subset of
your character array that begins with the character at the index specified bystartand is
numCharslong.
The following example uses a pair ofCharArrayReaders:
// Demonstrate CharArrayReader.
import java.io.*;
public class CharArrayReaderDemo {
public static void main(String args[]) throws IOException {
String tmp = "abcdefghijklmnopqrstuvwxyz";
int length = tmp.length();
char c[] = new char[length];
tmp.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 5);
int i;
System.out.println("input1 is:");
while((i = input1.read()) != -1) {
System.out.print((char)i);
}
System.out.println();
System.out.println("input2 is:");
while((i = input2.read()) != -1) {
System.out.print((char)i);
}
System.out.println();
}
}
Theinput1object is constructed using the entire lowercase alphabet, whileinput2contains
only the first five letters. Here is the output:
input1 is:
abcdefghijklmnopqrstuvwxyz
input2 is:
abcde
CharArrayWriter
CharArrayWriteris an implementation of an output stream that uses an array as the destination.
CharArrayWriterhas two constructors, shown here: