Java.io - SPLessons

Java.io InputStreamReader

Home > Lesson > Chapter 23
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io InputStreamReader

Java.io InputStreamReader

shape Introduction

The Java.io InputStreamReader is the class used to turn the byte (which is in the form of text) into the characters. Here the text will be in UTF - 8 format, so to read the characters in that format use input stream reader which will wraps the file input stream.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io InputStreamReader class is declared below: Public abstract class InputStreamReader extends Reader

shape Fields

Java.io InputStreamReader class fields are:
  • Protected Object Lock : For synchronize operations this object is used in InputStreamReader.

Class Constructors

shape Table

Constructor Description
InputStreamReader(InputStream in) The function of this constructor is to create the input stream reader instance.
InputStreamReader(InputStream in, Charset cs) The function of this constructor is to create the input stream reader instance with specified charset.
InputStreamReader (InputStream in, String CharsetName ) The function of this constructor is to create the input stream reader instance with specified name of charset.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
Boolean ready () The function of this method is to check the stream is ready to read or not.
int read(Char[] cbuf, int off, int n) By using this method it reads the data up to ‘n’ bytes from the character stream into an array of characters.
String getEncoding() The function of this method is to get the name of encoded character which was used by this input stream.
int read() The function of this method is to read only one character.

Inherited Methods

shape Description

From the following classes, methods are inherited to the input stream reader class.
  • Java.io.Object
  • Java.io.Reader

shape Examples

Usage of void close() method. [c]import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReader { public static void main(String[] args) throws IOException { FileInputStream fis = null; InputStreamReader isr =null; int i; char c; try { // creating a new input stream reader fis = new FileInputStream("D:/test.txt"); isr = new InputStreamReader(fis); // To close the input stream reader isr.close(); System.out.print("close() method accessed"); // To call read() method i=isr.read(); c=(char)i; System.out.println(c); } catch (Exception e) { // print if any error occurs //System.out.print("stream closed"); } finally { // It closes the stream and releases all the resources associated with this stream if(fis!=null) fis.close(); if(isr!=null) isr.close(); } } }[/c] Output The result will be as follows. [c]close() method accessed[/c] Usage of Boolean ready () method. [c]import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReader { public static void main(String[] args) throws IOException { FileInputStream fis = null; InputStreamReader isr =null; boolean bl = false; int i; char c; try { // Creating a new input stream reader fis = new FileInputStream("D:/test.txt"); isr = new InputStreamReader(fis); // To read into the char buffer while((i=isr.read())!=-1) { // To converts integer to character c=(char)i; // To print the character System.out.println("Character read: "+c); // It returns true if the next read is permitted bl = isr.ready(); // To print System.out.println("Ready to read: "+bl); } } catch (Exception e) { // print error e.printStackTrace(); } finally { // It closes the stream and releases all the resources associated with this stream if(fis!=null) fis.close(); if(isr!=null) isr.close(); } } }[/c] Output The result will be as follows. [c]Character read: S Ready to read: true Character read: P Ready to read: true Character read: Ready to read: true Character read: L Ready to read: true Character read: E Ready to read: true Character read: S Ready to read: true Character read: S Ready to read: true Character read: O Ready to read: true Character read: N Ready to read: true Character read: S Ready to read: false [/c]

Summary

shape Key Points

  • The Java.io InputStreamReader is the class used to turn the byte (which is in the form of text) into the characters.
  • Here the text will be in UTF - 8 format, so to read the characters in that format use input stream reader which will wraps the file input stream.