Java CharArrayReader class

Introduction

CharArrayReader implements an input stream to read from a character array.

This class has two constructors.

Both of the constructors requires a character array to provide the data source:

CharArrayReader(char array [ ])  
CharArrayReader(char array [ ], int start, int numChars) 

import java.io.CharArrayReader;
import java.io.IOException;

public class Main {
   public static void main(String args[]) {
      String tmp = "abcdefghijklmnopqrstuvwxyz";
      int length = tmp.length();
      char c[] = new char[length];

      tmp.getChars(0, length, c, 0);//from   w  ww .j av a 2  s  .co  m
      int i;

      try (CharArrayReader input1 = new CharArrayReader(c)) {
         System.out.println("input1 is:");
         while ((i = input1.read()) != -1) {
            System.out.print((char) i);
         }
         System.out.println();
      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }

      try (CharArrayReader input2 = new CharArrayReader(c, 0, 5)) {
         System.out.println("input2 is:");
         while ((i = input2.read()) != -1) {
            System.out.print((char) i);
         }
         System.out.println();
      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }
   }
}



PreviousNext

Related