CharArrayReader example program in Java

By: Abinaya Viewed: 153340 times  Printer Friendly Format    

CharArrayReader is 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(char array[ ])
CharArrayReader(char array[ ], int start, int numChars)

Here, array is the input source. The second constructor creates a Reader from a subset of your character array that begins with the character at the index specified by start and is numChars long.
The following example uses a pair of CharArrayReaders:

// Demonstrate CharArrayReader.
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 = != -1) {
System.out.println("input2 is:");
while((i = != -1) {

The input1 object is constructed using the entire lowercase alphabet, while input2 contains only the first five letters. Here is the output:

input1 is:
input2 is:

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial