Java Chararrayreader Class - Java Tutorial

Java Chararrayreader Class

BLUF: Mastering Java Chararrayreader Class is a key requirement for any Java developer. This lesson breaks down the object-oriented principles and syntax required to use this concept in real-world applications.
Write Once, Run Anywhere Tip: Java Chararrayreader Class

Java's versatility is unmatched. Learn how Java Chararrayreader Class fits into the Java ecosystem and improves your code structure in the tutorial below.

The CharArrayReader is a combination of CharArray and Reader. Utilized for reading a character array as a stream, the CharArrayReader class is an inheritor of the Reader class.

Java CharArrayReader class declaration

Now, let's examine the declaration of the Java.io.CharArrayReader class:

Example

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 explore a basic illustration demonstrating how to retrieve a character using the Java CharArrayReader class.

Example

package com.example;



import java.io.CharArrayReader;

public class CharArrayExample{

  public static void main(String[] ag) throws Exception {

    char[] ary = { 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't' };

    CharArrayReader reader = new CharArrayReader(ary);

    int k = 0;

    // Read until the end of a file

    while ((k = reader.read()) != -1) {

      char ch = (char) k;

      System.out.print(ch + " : ");

      System.out.println(k);

    }

  }

}

Output

Output

j : 106

a : 97

v : 118

a : 97

t : 116

p : 112

o : 111

i : 105

n : 110

t : 116

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience