Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io Reader

Java.io Reader

shape Introduction

The Java.io Reader is the class used for reading the content in the form of text like characters rather than bytes. The Java.io Reader is the super class to all the reader classes available in java. The reader is similar to that input stream class the only difference is that reader is used to read characters where as input stream reads the bytes of data.

shape Conceptual figure

Following is the types of readers.

Class Declaration

shape Declaration

Java.io Reader class is declared below: Public class Reader extends Object implements DataOutput, DataInput, Closeable

shape Fields

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

Class Constructors

shape Table

Constructor Description
Protected Reader () The function of this constructor is to create the reader instance and used to read character stream.
Protected Reader(Object lock) The function of this constructor is to create the reader instance and synchronize the sections in the given object.

Class Methods

shape Table

Method Description
abstract void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream 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 character buffer.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ‘ m ‘ characters.
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 reader class.
  • Java.io.Object

shape Examples

Usage of abstract void close() method. [c]import java.io.*; public class Reader{ public static void main(String[] args) { String s = "SP LESSONS"; // creating a new StringReader Reader reader = new StringReader(s); try { // To read the first eight characters for (int i = 0; i < 8; i++) { char c = (char) reader.read(); System.out.println("" + c); } // To close the stream reader.close(); System.out.println("Stream Closed."); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result. [c] S P L E S S O Stream Closed. [/c] Usage of Boolean markSupported () method. [c]import java.io.*; public class Reader { public static void main(String[] args) { String s = "SP LESSONS"; // creating a new StringReader Reader reader = new StringReader(s); try { // read the first five chars for (int i = 0; i < 9; i++) { char c = (char) reader.read(); System.out.print("" + c); } // To change line System.out.println(); // To check whether mark is supported or not System.out.println("" + reader.markSupported()); // To close the stream reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result. [c] SP LESSON true [/c]

Summary

shape Key Points

  • The reader is the class used for reading the content in the form of text like characters rather than bytes.
  • The reader is the super class to all the reader classes available in java.
  • The reader is similar to that input stream class the only difference is that reader is used to read characters where as input stream reads the bytes of data.