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:
CharArrayReader read(char[]) method in Java with Examples
Next article icon

CharArrayReader close() method in Java with Examples

Last Updated : 13 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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 effect.
  • If any read or other similar operation is performed on the stream, after closing, it raises IOException

Syntax:

public abstract void close()

Parameters: This method does not accepts any parameters

Return Value: This method do not returns any value.

Exception: This method throws IOException if some error occurs while input output.

Below methods illustrates the working of close() method:

Program 1:




// Java program to demonstrate
// CharArrayReader close() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            char[] str = { 'G', 'e', 'e', 'k', 's',
                           'F', 'o', 'r',
                           'G', 'e', 'e', 'k', 's' };
  
            // Create a CharArrayReader instance
            CharArrayReader reader
                = new CharArrayReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 5 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 5; i++) {
                ch = reader.read();
                System.out.println("\nInteger value "
                                   + "of character read: "
                                   + ch);
                System.out.println("Actual "
                                   + "character read: "
                                   + (char)ch);
            }
  
            // Close the stream using close()
            reader.close();
            System.out.println("Stream Closed.");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
 
 
Output:
  Integer value of character read: 71  Actual character read: G    Integer value of character read: 101  Actual character read: e    Integer value of character read: 101  Actual character read: e    Integer value of character read: 107  Actual character read: k    Integer value of character read: 115  Actual character read: s  Stream Closed.  

Program 2:




// Java program to demonstrate
// CharArrayReader close() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            char[] str = { 'G', 'e', 'e', 'k', 's' };
  
            // Create a CharArrayReader instance
            CharArrayReader reader
                = new CharArrayReader(str);
  
            // Close the stream using close()
            reader.close();
            System.out.println("Stream Closed.");
  
            // Check if the CharArrayReader is
            // ready to be read using ready()
            System.out.println("Is CharArrayReader ready "
                               + "to be read"
                               + reader.ready());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
 
 
Output:
  Stream Closed.  java.io.IOException: Stream closed  

Reference: https://docs.oracle.com/javase/9/docs/api/java/io/CharArrayReader.html#close–



Next Article
CharArrayReader read(char[]) method in Java with Examples

C

code_r
Improve
Article Tags :
  • Java
  • Java-CharArrayReader
  • Java-Functions
  • Java-IO package
Practice Tags :
  • Java

Similar Reads

  • CharArrayReader read() method in Java with Examples
    The read() method of CharArrayReader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract me
    3 min read
  • CharArrayReader ready() method in Java with Examples
    The ready() method of CharArrayReader Class in Java is used to check whether this CharArrayReader is ready to be read or not. It returns a boolean which states if the reader is ready. Syntax: public void ready() Parameters: This method does not accepts any parameters Return Value: This method return
    3 min read
  • CharArrayReader reset() method in Java with Examples
    The reset() method of CharArrayReader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting. Syntax: public void reset() Parameters: This method do not accepts any
    3 min read
  • BufferedReader close() method in Java with Examples
    The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return a
    2 min read
  • CharArrayReader read(char[]) method in Java with Examples
    The read(char[]) method of CharArrayReader Class in Java is used to read the specified characters into an array. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char
    2 min read
  • CharArrayReader read(CharBuffer) method in Java with Examples
    The read(CharBuffer) method of CharArrayReader Class in Java is used to read the specified characters into a CharBuffer instance. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: pub
    2 min read
  • CharArrayReader mark(int) method in Java with Examples
    The mark() method of CharArrayReader Class in Java is used to mark the stream as the checkpoint from where the stream read will start, once reset() is called. This method is not supported by all subclasses of CharArrayReader class. Syntax: public void mark(int readAheadLimit) Parameters: This method
    3 min read
  • CharArrayReader skip(long) method in Java with Examples
    The skip(long) method of CharArrayReader Class in Java is used to skip the specified number of characters on the stream. This number of characters is specified as the parameter. If it reaches the end of the stream by skipping, it will block the stream till it gets some characters, or throw IOExcepti
    3 min read
  • CharArrayWriter size() method in Java with examples
    The size() method of the CharArrayWriter class in Java is used to get the current size of the buffer. Syntax: public int size() Parameters: This method does not accept any parameter.Return Value: This method returns an integer value which signifies the current size of the buffer. Below program illus
    2 min read
  • ByteArrayInputStream close() method in Java with Examples
    The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector. Syntax: public void close() Parameters: The function does not accepts any parameter. Return Value: The function returns
    2 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