PUBLISHED ON: FEBRUARY 24, 2021
Java PushbackReader Class
In this tutorial, we will learn about PushbackReader
class in Java. This class is a character stream class and it allows characters to be pushed back into the Stream. The unread() method of this class allows us to pushback the characters.
Syntax
This is the syntax declaration of PushbackReader
class and this class extends FilterReader
class.
public class PushbackReader extends FilterReader
PushbackReader Class Constructor
Constructors of PushbackReader class given in the table below:
Constructor |
Description |
PushbackReader(Reader in) |
This creates a new pushback reader with a one-character pushback buffer. |
PushbackReader(Reader in, int size) |
This creates a new pushback reader with a pushback buffer of the given size. |
PushbackReader Class Methods
All the methods of this class are given in the table below.
Method |
Description |
int read() |
This method is used to read a single character. |
void mark(int readAheadLimit) |
This method is used to mark the present position in a stream. |
boolean ready() |
This method is used to tell whether the stream is ready to be read. |
boolean markSupported() |
This method is used to tell whether the stream supports mark() operation. |
long skip(long n) |
This method is used to skip the character. |
void unread (int c) |
This method is used to pushes back the character by copying it to the pushback buffer. |
void unread (char[] cbuf) |
This method is used to pushes back an array of character by copying it to the pushback buffer. |
void reset() |
This method is used to reset the stream. |
void close() |
This method is used to close the stream. |
Example of PushbackReader Class
In the example given below, we are discussing read()
and skip()
method. Here we can see, we have string str, using the string reader of this string we created an object of PushbackReader class. Now we apply the read()
method to read each character from the string but we also implemented skip()
method so it will print the alternative characters.
package studytonight;
import java.io.PushbackReader;
import java.io.StringReader;
public class StudyTonight
{
public static void main(String args[])
{
try
{
String str = "Hello Studytonight";
StringReader reader = new StringReader(str);
PushbackReader pushbackReader = new PushbackReader(reader);
System.out.println("pushbackReader ready: " + pushbackReader.ready());
for (int i = 0; i < 9; i++)
{
char ch = (char) pushbackReader.read();
System.out.print(ch);
pushbackReader.skip(1);
}
}
catch (Exception e) {
System.out.print("Error: "+e.toString());
}
}
}
pushbackReader ready: true
HloSuyoih
Example of PushbackReader Class
In this example, we are implementing the unread() method of PushbackReader class, here we are reading the first 5 characters the same as we did in the above article. After reading 7 characters from the string we are pushing s using the unread method and when we try to read a new character using the read() method it will return the same character.
package studytonight;
import java.io.PushbackReader;
import java.io.StringReader;
public class StudyTonight
{
public static void main(String args[])
{
String str = "Student of this class";
StringReader reader = new StringReader(str);
PushbackReader pushbackReader = new PushbackReader(reader, 20);
try
{
for (int i = 0; i < 7; i++)
{
char ch = (char) pushbackReader.read();
System.out.print("" + ch);
}
pushbackReader.unread('s');
char ch = (char) pushbackReader.read();
System.out.println("" + ch);
pushbackReader.close();
}
catch (Exception e)
{
System.out.print("Error: "+e.toString());
}
}
}
Students