CharArrayReader class

 

                  see  Reader class  for descriptions of the seven methods CharArrayReader can use.  They are:

 

.mark( readAheadLimit )  Mark the present position in the stream.

.markSupported( ) Tell whether this stream supports the mark(...) operation.

.read( )  Read a single character, returning it in the low end two bytes of an int.

.read( char[ ] )  Read characters into a char array.

.read( char[ ], offset, len)  Read characters into a portion of a char array.

.ready( ) Tell whether this stream is ready to be read.

.reset( )  Reset the stream.

.skip( n )  Skip over n characters in the stream.

.close( ) Close the stream.

 

  CharArrayReader is a character input stream that uses a char array, specified in its constructor, as the source of characters which its methods return. It reads from a char array, in other words.

 

  CharArrayReader supports mark( ) and reset( ).

 

  CharArrayReader's constructors do not throw IOExceptions, but its methods do.  There are two CharArrayReader constructors. 

 

  This snippet reads and prints the whole c array.

 

import java.io.*;

 

char[ ] c = { 'A', 'B', 'C', 'D', 'E', '1', '2', '3', '4', '5'} ;

CharArrayReader car = new CharArrayReader( c );                          // constructor indicates whole array to read

int x;

try {

   while ( (x = car.read( ) ) != -1 ) { 

       System.out.print( (char) x );

    }

} catch (IOException e) {   }

 

 

  This snippet uses the other CharArrayReader constructor.  As a result it reads and prints just 12345.  The offset and length parameters in the constructor dictate where CharArrayReader will start reading and where end of file will occur.

 

import java.io.*;

 

char[ ] c = { 'A', 'B', 'C', 'D', 'E', '1', '2', '3', '4', '5'} ;

CharArrayReader car = new CharArrayReader( c, 5, 5 );  // constructor says offset 5 and length 5

int x;

try {

   while ( (x = car.read( ) ) != -1 ) { 

       System.out.print( (char) x );

    }

} catch (IOException e) {   }