Java.io - SPLessons

Java.io – PushbackReader

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

Java.io – PushbackReader

Java.io PushbackReader

shape Introduction

The Java.io PushbackReader is a class used to unread data, to push back characters into stream. The main functionality of this is to interpret the present  character into the reader. If the unread() method is called then, it pushes the data into the push back reader. After again calling read() method at that time the push backed data will reads first. The last pushed character will be reads first. The push back data limit is also initialized with constructor in push back reader.

Class Declaration

shape Declaration

Java.io PushbackReader class is declared below: Public class PushbackReader extends FilterReader

shape Fields

Java.io PushbackReader class fields are:
  • Protected Reader in : The field represents the input stream have to read.
  • Protected Object Lock : For synchronize operations this object is used in PushBackReader.

Class Constructors

shape Table

Constructor Description
PushBackReader (Reader in) The function of this constructor is to create the push back reader instance and used to read character into buffer.
PushBackReader (Reader in, int size) The function of this constructor is to create the push back reader instance and used to read character into the represented size of buffer.

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 markSupported () The function of this method is to check the represented mark method is supported by this stream or not.
void unread(char[] cbuf, int off, int n) The function of this method is to push back the specified length of array of characters  from the stream and copied into push back buffer.
void unread(char[] cbuf) The function of this method is to push back the characters and copied into push back buffer.

Inherited Methods

shape Description

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

shape Examples

Usage of void unread(char[] cbuf) method. [c]import java.io.*; public class PushbackReader { public static void main(String[] args) { String s = "SPLESSONS"; // creating a new String reader instance StringReader sr = new StringReader(s); // creating a new Push back reader based on string reader PushbackReader pr = new PushbackReader(sr, 20); try { // To read the characters for (int i = 0; i < 9; i++) { char c = (char) pr.read(); System.out.print("" + c); } // To change line System.out.println(); // creating a new array to unread char cbuf[] = {'w', 'o', 'r', 'l', 'd'}; // unread into character buffer pr.unread(cbuf); // To read five characters, which is what we unread from cbuf for (int i = 0; i < 5; i++) { char c = (char) pr.read(); System.out.print("" + c); } // To close the stream pr.close(); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c] SPLESSONS world [/c] Usage of Boolean markSupported () method. [c]import java.io.*; public class PushbackReader { public static void main(String[] args) { String s = "SPLESSONS"; // creating a new String reader instance StringReader sr = new StringReader(s); // creating a new Push back reader based on the string reader PushbackReader pr = new PushbackReader(sr, 20); try { // To read the given characters for (int i = 0; i < 9; i++) { char c = (char) pr.read(); System.out.print("" + c); } // To change line System.out.println(); // To close the stream pr.close(); System.out.println("Stream closed."); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c] SPLESSONS Stream closed. [/c]

Summary

shape Key Points

  • TheJava.io PushbackReader is a class used to unread data i.e., to push back characters into stream.
  • The main functionality of this is to interpret the present  character into the reader.
  • When the unread() method is called then, it pushes the data into the push back reader.
  • The last pushed character will be reads first. The push back data limit is also initialized with constructor in Java.io PushbackReader.