Java - CharArrayReader Class



Introduction

The Java CharArrayReader class implements a character buffer that can be used as a character-input stream.

Class declaration

Following is the declaration for Java.io.CharArrayReader class −

public class CharArrayReader
   extends Reader

Field

Following are the fields for Java.io.CharArrayReader class −

  • protected char[] buf − This is the character buffer.

  • protected int count − This is the index of the end of this buffer.

  • protected int markedPos − This is the position of mark in buffer.

  • protected int pos − This is the current buffer position.

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

CharArrayReader(char[] buf)

This creates a CharArrayReader from the specified array of chars.

2

CharArrayReader(char[] buf, int offset, int length)

This creates a CharArrayReader from the specified array of chars.

Class methods

Sr.No. Method & Description
1 void close()

This method closes the stream and releases any system resources associated with it.

2 void mark(int readAheadLimit)

This method marks the present position in the stream.

3 boolean markSupported()

This method tells whether this stream supports the mark() operation, which it does.

4 int read()

This method reads a single character.

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

This method Reads characters into a portion of an array.

6 boolean ready()

This method tells whether this stream is ready to be read.

7 void reset()

This method resets the stream to the most recent mark, or to the beginning if it has never been marked.

8 long skip(long n)

This method skips characters.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Reader
  • Java.io.Object

Example - Closing the Stream After Use

The following example shows the usage of Java CharArrayReader close() method.

CharArrayReaderDemo.java

package com.tutorialspoint;

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

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Hello, World!".toCharArray();

      // Create a CharArrayReader
      CharArrayReader reader = new CharArrayReader(data);

      try {
         // Read and print characters
         System.out.println("Reading characters:");
         int charData;
         while ((charData = reader.read()) != -1) {
            System.out.print((char) charData);
         }

         // Close the reader
         reader.close();
         System.out.println("\nCharArrayReader closed.");
      } catch (IOException e) {
         System.err.println("An IOException occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Reading characters:
Hello, World!
CharArrayReader closed.

Explanation

  • A CharArrayReader is created using a character array containing "Hello, World!".

  • Characters are read and printed one by one until the end of the stream.

  • The close() method is called to formally close the reader after use.

  • Since CharArrayReader doesn't use external resources, closing is optional but recommended for consistency.

Example - Basic Use of mark(int readAheadLimit)

The following example shows the usage of Java CharArrayReader mark(readAheadLimit) method.

CharArrayReaderDemo.java

package com.tutorialspoint;

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

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Hello, World!".toCharArray();

      // Create a CharArrayReader
      CharArrayReader reader = new CharArrayReader(data);

      try {
         // Read and print the first three characters
         System.out.print("Initial characters: ");
         for (int i = 0; i < 3; i++) {
            System.out.print((char) reader.read());
         }
         System.out.println();

         // Mark the current position
         reader.mark(0); // The readAheadLimit is ignored in CharArrayReader
         System.out.println("Marked current position.");

         // Read and print the next three characters
         System.out.print("Next characters: ");
         for (int i = 0; i < 3; i++) {
            System.out.print((char) reader.read());
         }
         System.out.println();

         // Reset the stream to the marked position
         reader.reset();
         System.out.println("Stream reset to the marked position.");

         // Read and print the same three characters again
         System.out.print("Characters after reset: ");
         for (int i = 0; i < 3; i++) {
            System.out.print((char) reader.read());
         }
         System.out.println();
      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial characters: Hel
Marked current position.
Next characters: lo,
Stream reset to the marked position.
Characters after reset: lo,

Explanation

  • The first three characters (Hel) are read and printed.

  • The mark() method is called to mark the current position after reading three characters.

  • After reading the next three characters (lo,), the reset() method is called to return the stream to the marked position.

  • The same three characters (lo,) are read again after the reset.

Example - Simple Check for markSupported()

The following example shows the usage of Java CharArrayReader mark(readAheadLimit) method.

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Hello, World!".toCharArray();

      // Create a CharArrayReader
      CharArrayReader reader = new CharArrayReader(data);

      // Check if mark is supported
      if (reader.markSupported()) {
         System.out.println("Mark and reset are supported by CharArrayReader.");
      } else {
         System.out.println("Mark and reset are NOT supported by CharArrayReader.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Mark and reset are supported by CharArrayReader.

Explanation

  • A CharArrayReader is created using a character array containing "Hello, World!".

  • The markSupported() method is called to check if the mark() and reset() methods are supported.

  • Since CharArrayReader always supports marking, the output will confirm that "Mark and reset are supported by CharArrayReader."

Advertisements