Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
InputStreamReader class in Java
Next article icon

InputStreamReader class in Java

Last Updated : 21 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
InputStreamReader class

An InputStreamReader is a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted. 
Declaration : 

public class InputStreamReader    extends Reader


Constructors : 

  • InputStreamReader(InputStream in_strm) : Creates an InputStreamReader that uses the default charset.
  • InputStreamReader(InputStream in_strm, Charset cs) : creates an InputStreamReader that uses the given charset.
  • InputStreamReader(InputStream in_strm, CharsetDecoder dec) : Creates an InputStreamReader that uses the given charset decoder.
  • InputStreamReader(InputStream in_strm, String charsetName) : Creates an InputStreamReader that uses the named charset


Methods: 

  • ready() : java.io.InputStreamReader.ready() tells whether the Character stream is ready to be read or not. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream. 
    Syntax : 
public boolean ready() Returns : True : if the Character stream is ready to be read False : if the Character stream is not ready to be read
  • close() : java.io.InputStreamReader.close() closes InputStreamReader and releases all the Streams associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. 
    Syntax : 
public void close() Returns : No value is returned
  • Implementation of ready() and close() method : 
Java
// Java program illustrating ready() and close() method  import java.io.*; public class NewClass {     public static void main(String[] args)     {          try         {             // initializing FileInputStream             FileInputStream geek = new FileInputStream("ABC.txt");              // Initializing InputStreamReader object             InputStreamReader in_strm = new InputStreamReader(geek);              int t;             while((t=in_strm.read())!= -1)             {                 // convert the integer true to character                 char r = (char)t;                 System.out.println("Character : "+r);                  // check if the stream in_strm ready                 boolean b = in_strm.ready();                 // Use of ready() methods                 System.out.println("Ready? : "+b);              }                          // Use of close() method to Close InputStreamReader             in_strm.close();                          // Closing FileInputStream             geek.close();         }         catch (FileNotFoundException fnfe)         {             System.out.println("NO Such File Exists");         }         catch (IOException except)         {             System.out.println("IOException occurred");         }     } } 
  • Note : 
    All the programs in this article won’t run on online IDE as no ‘ABC’ file exists. You can check this code on Java compiler on your system. 
    To check this code, create a file ‘ABC’ on your system. 
    ‘ABC’ file contains : 
    Geeks 
    For 
    Geeks 
    Output : 
Character : G Ready? : true Character : e Ready? : true Character : e Ready? : true Character : k Ready? : true Character : s Ready? : true Character :   Ready? : true Character :  Ready? : true Character :   Ready? : true Character : F Ready? : true Character : o Ready? : true Character : r Ready? : true Character :   Ready? : true Character :  Ready? : true Character :   Ready? : true Character : G Ready? : true Character : e Ready? : true Character : e Ready? : true Character : k Ready? : true Character : s Ready? : false
  • getEncoding() : java.io.InputStreamReader.getEncoding() returns the name of the character encoding being used by this stream. 
    Syntax : 
public String getEncoding() Parameters :  Returns : No value is returned
  • Implementation of getEncoding() method : 
Java
// Java program illustrating getEncoding() method  import java.io.*; public class NewClass {     public static void main(String[] args)     {          try         {             // initializing FileInputStream             FileInputStream geek = new FileInputStream("ABC.txt");              // Initializing InputStreamReader object             InputStreamReader in_strm = new InputStreamReader(geek);              // Use of getEncoding() method             // to get the character encoding present in the stream             String encoding = in_strm.getEncoding();              System.out.println("Encoding used : "+encoding);              // Closing InputStreamReader             in_strm.close();                          // Closing FileInputStream             geek.close();         }         catch (FileNotFoundException fnfe)         {             System.out.println("NO Such File Exists");         }         catch (IOException except)         {             System.out.println("IOException occurred");         }     } } 
  • Output : 
Encoding used : UTF8
  • read() : java.io.InputStreamReader.read() Returns single character after reading. 
    Syntax : 
public int read() Returns : Returns single character after reading or -1 if the end of the stream has been reached
  • Implementation : 
Java
// Java program illustrating read() method  import java.io.*; public class NewClass {     public static void main(String[] args) throws FileNotFoundException, IOException     {          // initializing FileInputStream         FileInputStream geek = new FileInputStream("ABC.txt");          // Initializing InputStreamReader object         InputStreamReader in_strm = new InputStreamReader(geek);          int t;         String read_reslt="";          // Use of read() method         while((t = in_strm.read()) != -1)         {             read_reslt = read_reslt+(char)t;         }              // print the result read from the file         System.out.println(read_reslt);     } } 
  • Note : 
    ‘ABC’ file contains : 
    1 
    Geeks 
    2 
    For 
    3 
    Geeks 
    Output : 
1 Geeks  2 For  3 Geeks


Next Article
InputStreamReader class in Java

M

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

Similar Reads

    Java FileInputStream Class
    FileInputStream class in Java is useful for reading data from a file in the form of a Java sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.Example: FileInputStream class to read data from fi
    4 min read
    Java.io.InputStream Class in Java
    Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is i
    3 min read
    Java FileReader Class
    FileReader in Java is a class in the java.io package which can be used to read a stream of characters from the files. Java IO FileReader class uses either specified charset or the platform's default charset for decoding from bytes to characters.1. Charset: The Charset class is used to define methods
    3 min read
    Java.io.FilterInputStream Class in Java
    Filter Streams filters data as they read and write data in the Input Stream, filters it and pass it on to the underlying Streams. Filter Streams are FilterInputStream FilterOutput Stream FilterInputStream : Java.io.FilterInputStream class works almost like InputStream class in Java but what it does
    9 min read
    Java.io.DataInputStream class in Java | Set 2
    Java.io.DataInputStream class in Java | Set 1 More Methods: byte readByte() : Reads and returns one input byte. Syntax:public final byte readByte() throws IOException Returns: the next byte of this input stream as a signed 8-bit byte. Throws: EOFException IOException float readFloat() : Reads four i
    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