VOOZH about

URL: https://www.geeksforgeeks.org/java/java-io-chararrayreader-class-java/

⇱ Java.io.CharArrayReader Class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.io.CharArrayReader Class in Java

Last Updated : 22 Oct, 2025

The CharArrayReader class in Java is part of the java.io package and allows you to read characters from a character array (char[]) as a stream. It extends the Reader class and is mainly used when you want to treat a character array as an input source, similar to reading from files or other streams.

Class Declaration

public class CharArrayReader extends Reader

  • Package: java.io
  • Superclass: Reader
  • Implements: Closeable, AutoCloseable

Constructors of CharArrayReader

CharArrayReader provides two constructors

  • CharArrayReader(char[] char_array): Creates a CharArrayReader using the entire character array.
  • CharArrayReader(char[] char_array, int offset, int maxlen): Creates a CharArrayReader starting from a specific offset and reading only maxlen characters.

Important Methods of CharArrayReader

1. read()

Reads a single character from the input stream and returns it as an integer


Output
J A V A 

2. read(char[] buffer, int offset, int length)

Reads characters into a portion of the specified array from the given offset for a given length.


Output
HEL

3. ready()

Checks whether the stream is ready to be read. Since it’s memory-based, it’s always ready unless closed.


Output
Reader is ready to read.

4. skip(long n)

Skips the specified number of characters in the stream.


Output
C D E 

5. mark(int readAheadLimit)

Marks the current position in the stream so it can be returned to later using reset().


Output
XYZ
After reset: Y Z P Q 

6. markSupported()

Returns true indicating that the mark() and reset() methods are supported.


Output
markSupported(): true

7. reset()

Resets the stream to the most recently marked position or to the beginning if no mark was set.


Output
HEL
After reset: E L L O 

8. close()

Closes the reader and releases resources.


Output
Reader closed successfully.



Comment
Article Tags:
Article Tags: