Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java CharArrayWriter Class | Set 1
Next article icon

Java.io.CharArrayReader Class in Java

Last Updated : 18 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

CharArrayReader Class in Java

java.io.CharArrayReader class creates a character buffer using a character array.

Declaration: 

public class CharArrayReader    extends Reader

Constructor :  

  • CharArrayReader(char[] char_array) : Creates a CharArrayReader from a specified character array.
  • CharArrayReader(char[] char_array, int offset, int maxlen) : Creates a CharArrayReader from a specified part of character array.

Methods:  

  • read() : java.io.CharArrayReader.read() reads a single character and returns  -1 if end of the Stream is reached. 
    Syntax : 
public int read() Parameters :  ----------- Return  : Returns read character as an integer ranging from range 0 to 65535. -1 : when end of file is reached.
  • read(char[] char_array, int offset, int maxlen) : java.io.CharArrayReader.read(char[] char_array, int offset, int maxlen)) reads a single character and returns -1 if end of the Stream is reached 
    Syntax : 
public int read(char[] char_array, int offset, int maxlen)) Parameters :  char_array : destination array   offset : starting position from where to store characters maxlen : maximum no. of characters to be read Return  : Returns all the characters read -1 : when end of file is reached.
  • ready() : java.io.CharArrayReader.ready() checks whether the Stream is ready to be read or not. 
    CharArrayReader are always ready to be read. 
    Syntax : 
public boolean ready() Parameters :  ----------- Return  : true if CharArrayReader is ready to be read.
  • skip(long char) : java.io.CharArrayReader.skip(long char_no) skips ‘char_no’ no. of characters.  If n is negative, then this method does nothing and returns 0. 
    Syntax : 
public long skip(long char) Parameters :  char_no : char no. of characters to be skipped Return  : no. of characters skipped Exception :  IOException : In case of I/O error occurs

Java




// Java program illustrating the working of CharArrayReader class methods
// read(), skip(), ready()
// read(char[] char_array, int offset, int maxlen)
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
 
        // Initializing the character array
        char[] geek = {'G', 'E', 'E', 'K', 'S'};
 
        // Initializing the char_array
        CharArrayReader char_array1 = new CharArrayReader(geek);
        CharArrayReader char_array2 = new CharArrayReader(geek);
 
        // Use of ready() method
        boolean check1 = char_array1.ready();
        if(check1 ==true)
            System.out.println("char_array1 is ready");
        else
            System.out.println("char_array1 is not ready");
 
 
        int a = 0;
        System.out.print("Use of read() method : ");
        // Use of read() method : reading each character one by one
        while((a = char_array1.read()) != -1)
        {
            char c1 = (char)a;
            System.out.println(c1);
 
            // Use of skip() method
            long char_no = char_array1.skip(1);
            System.out.println("Characters Skipped : "+(c1+1));
 
        }
        System.out.println("");
 
 
        // Use of ready() method
        boolean check2 = char_array2.ready();
        if(check2 ==true)
            System.out.println("char_array2 is ready");
        else
            System.out.println("char_array2 is not ready");
 
 
  // Use of read(char[] char_array, int offset, int maxlen) : reading a part of array
        char_array2.read(geek, 1, 2);
 
        int b = 0;
 
  System.out.print("Use of read(char[] char_array, int offset, int maxlen) method : ");
 
        while((b = char_array2.read()) != -1)
        {
            char c2 = (char)b;
            System.out.print(c2);
        }
 
    }
}
 
 

Output : 

char_array1 is ready Use of read() method : G Characters Skipped : 72 E Characters Skipped : 70 S Characters Skipped : 84  char_array2 is ready Use of read(char[] char_array, int offset, int maxlen) method : EKS
  • mark(int readLimit) : java.io.CharArrayReader.mark(int readLimit) marks the current position in the Stream upto which the character can be read. This method always invokes reset() method. Subsequent calls to reset() will reposition the stream to this point. 
    Syntax : 
public long mark(int readLimit) Parameters :  readLimit : No. of characters that can be read up to the mark Return  : void Exception :  IOException : In case of I/O error occurs
  • markSupported() : java.io.CharArrayReader.markSupported() tells whether the mark method is supported by the stream or not. 
    Syntax : 
public boolean markSupported() Parameters :  ------- Return  : true if the mark method is supported by the stream Exception :  IOException : In case of I/O error occurs
  • reset() : java.io.CharArrayReader.reset() Resets the stream to the most recent mark, or to the beginning if it has never been marked. 
    Syntax : 
public void reset() Parameters :  ------- Return  : void Exception :  IOException : In case of I/O error occurs
  • close() : java.io.CharArrayReader.close() closes the stream and reallocates the resources that were allotted to it. 
    Syntax : 
public void close() Parameters :  ------- Return  : void Exception :  IOException : In case of I/O error occurs

Java




// Java program illustrating the working of FilterInputStream method
// mark(), reset()
// markSupported(), close()
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws Exception
    {
        // Initializing CharArrayReader
        CharArrayReader char_array = null;
        char[] geek = {'H', 'E', 'L', 'L', 'O', 'G',  'E',  'E',  'K', 'S'};
 
        try
        {
            char_array = new CharArrayReader(geek);
 
            // read() method : reading and printing Characters
            // one by one
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
 
            // mark() : read limiting the 'geek' input stream
            char_array.mark(0);
 
            System.out.println("mark() method comes to play");
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
            System.out.println("Char : "+(char)char_array.read());
 
            // Use of markSupported() :
            boolean check = char_array.markSupported();
            if (check == true )
                System.out.println("mark() supported\n");
 
            if (char_array.markSupported())
            {
                // reset() method : repositioning the stream to
                // marked positions.
                char_array.reset();
                System.out.println("reset() invoked");
                System.out.println("Char : "+(char)char_array.read());
                System.out.println("Char : "+(char)char_array.read());
            }
            else
                System.out.println("mark() method not supported.");
             
        }
        catch(Exception excpt)
        {
            // in case of I/O error
            excpt.printStackTrace();
        }
        finally
        {
            // Use of close() : releasing the resources back to the
            // GarbageCollector when closes
            if(char_array != null)
                char_array.close();
        }
    }
}
 
 

Output : 

Char : H Char : E Char : L mark() method comes to play Char : L Char : O Char : G mark() supported  reset() invoked Char : L Char : O

 



Next Article
Java CharArrayWriter Class | Set 1

M

Mohit Gupta
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

  • Java.io.ByteArrayInputStream class in Java
    ByteArrayInputStream class of java.io package contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class
    3 min read
  • Java.io.ByteArrayOutputStream() Class in Java
    java.io.ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class. Thu
    4 min read
  • java.nio.charset.CharsetEncoder Class in Java
    For the purpose of character encoding and decoding, java offers a number of classes in the 'java.nio.charset' package. The 'CharsetEncoder' class of this package performs the important task of encoding. In this article, let us understand this class, its syntax, different methods, and some examples o
    6 min read
  • Java.io.FilterReader class in Java
    Abstract class for reading filtered character streams. The abstract class FilterReader itself provides default methods that pass all requests to the contained stream. Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields. Constructor : pr
    3 min read
  • Java CharArrayWriter Class | Set 1
    The CharArrayWriter class in Java is part of the java.io package, and it is used to write data into a character array. This class is used when we want to store characters temporarily without dealing with files and other storage. Features of CharArrayWriter Class:, It is used to store characters in m
    6 min read
  • Java CharArrayWriter Class | Set 2
    In the Java.io.CharArrayWriter class in Java | Set 1, we have already discussed about which CharArrayWriter class and how it works. In this article, we are going to discuss some more methods of the CharArrayWriter class, which give us strong control over handling character data. Java CharArrayWriter
    3 min read
  • java.nio.charset.Charset Class in Java
    In Java, Charset is a mapping technique used in Java to map the 16-bit Unicode sequence and sequences of bytes. It is also used to encode and decode the string data text into different character encoding. It comes under java.nio.charset.Charset package. The charset must begin with a number or letter
    2 min read
  • CharsetDecoder Class in Java
    For encoding and decoding tasks, many methods are offered in Charset Encoder and Charset Decoder classes in Java. The Charset Decoder class is used for text handling to convert bytes to characters. The Charset decoder accepts a sequence of bytes as its input and displays Unicode characters as output
    5 min read
  • CharMatcher Class | Guava | Java
    CharMatcher determines a true or false value for any Java char value. This class provides various methods to handle various Java types for char values. Declaration: The declaration for com.google.common.base.CharMatcher is as: @GwtCompatible(emulated = true) public final class CharMatcher extends Ob
    3 min read
  • CharArrayReader close() method in Java with Examples
    The close() method of CharArrayReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effe
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences