JavaCharArrayReader

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java CharArrayReader Class

The CharArrayReader is composed of two words: CharArray and Reader. The CharArrayReader class is used to read character array as a reader (stream). It inherits Reader class.

Java CharArrayReader class declaration

Let's see the declaration for Java.io.CharArrayReader class:

  1. public class CharArrayReader extends Reader  

Java CharArrayReader class methods

Method

Description

int read()

It is used to read a single character

int read(char[] b, int off, int len)

It is used to read characters into the portion of an array.

boolean ready()

It is used to tell whether the stream is ready to read.

boolean markSupported()

It is used to tell whether the stream supports mark() operation.

long skip(long n)

It is used to skip the character in the input stream.

void mark(int readAheadLimit)

It is used to mark the present position in the stream.

void reset()

It is used to reset the stream to a most recent mark.

void close()

It is used to closes the stream.

Example of CharArrayReader Class:

Let's see the simple example to read a character using Java CharArrayReader class.

  1. package com.javatpoint;  
  2.   
  3. import java.io.CharArrayReader;  
  4. public class CharArrayExample{  
  5.   public static void main(String[] ag) throws Exception {  
  6.     char[] ary = { 'j''a''v''a''t''p''o''i''n''t' };  
  7.     CharArrayReader reader = new CharArrayReader(ary);  
  8.     int k = 0;  
  9.     // Read until the end of a file  
  10.     while ((k = reader.read()) != -1) {  
  11.       char ch = (char) k;  
  12.       System.out.print(ch + " : ");  
  13.       System.out.println(k);  
  14.     }  
  15.   }  
  16. }  

Output

j : 106
a : 97
v : 118
a : 97
t : 116
p : 112
o : 111
i : 105
n : 110
t : 116