PushbackReader unread() method in Java with Examples
The unread() method of PushbackReader class in Java is of three types:
- The unread(int c) method of PushbackReader class in Java is used to push back a character by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the parameter passed.
Syntax:
public void unread(int c)
throws IOException
Parameters: This method accepts one parameter c that represents the integer value of a character which is to be pushed back.
Return value: This method does not return any value.
Exceptions: This method throws IOException if the pushback buffer is full or if an I/O error occurs.
Below programs illustrate unread(int) method of PushbackReader class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
pushbackReader.unread( 65 );
System.out.print(
"\n"
+ ( char )pushbackReader.read());
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKSFORGEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
pushbackReader.unread( 'Z' );
System.out.print(
"\n"
+ ( char )pushbackReader.read());
}
}
|
- The unread(char[] cbuf) method of PushbackReader class in Java is used to push back an array of characters by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the first element of the character array.
Syntax:
public void unread(char[] cbuf)
throws IOException
Parameters: This method accepts one parameter cbuf that represents the character array which is to be pushed back.
Return value: This method does not return any value.
Exceptions: This method throws IOException if the pushback buffer is full or if an I/O error occurs.
Below programs illustrate unread(char[]) method of PushbackReader class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
char [] cbuf = new char [] { 'A' , 'B' , 'C' };
pushbackReader.unread(cbuf);
System.out.println();
for ( int i = 0 ; i < cbuf.length; i++) {
System.out.print(
( char )pushbackReader.read());
}
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKSFORGEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
char [] cbuf = new char [] { 'X' , 'Y' , 'Z' };
pushbackReader.unread(cbuf);
System.out.println();
for ( int i = 0 ; i < cbuf.length; i++) {
System.out.print(
( char )pushbackReader.read());
}
}
}
|
Output:
GEEKSFORGEEKS
XYZ
- The unread(char[] cbuf, int offset, int length) method of PushbackReader class in Java is used to push back a part of an array of characters by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the first element of the portion of the given character array.
Syntax:
public void unread(char[] cbuf,
int offset,
int length)
throws IOException
Parameters: This method accepts three parameters:
- cbuf – It represents the character array the portion of which is to be pushed.
- offset – It represents the starting index of the portion of character array.
- length – It represents the number of characters to be pushed.
Return value: This method does not return any value.
Exceptions: This method throws IOException if there is not sufficient space in the pushback buffer or if an I/O error occurs.
Below programs illustrate unread(char[], int, int) method of PushbackReader class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
char [] cbuf
= new char [] { 'A' , 'B' , 'C' ,
'D' , 'E' };
pushbackReader.unread(cbuf, 2 , 3 );
System.out.println();
for ( int i = 0 ; i < 3 ; i++) {
System.out.print(
( char )pushbackReader.read());
}
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
String str = "GEEKSFORGEEKS" ;
StringReader strReader
= new StringReader(str);
PushbackReader pushbackReader
= new PushbackReader(strReader, 100 );
for ( int i = 0 ; i < str.length(); i++) {
System.out.print(
( char )pushbackReader.read());
}
char [] cbuf
= new char [] { 'W' , 'X' ,
'Y' , 'Z' };
pushbackReader.unread(cbuf, 1 , 3 );
System.out.println();
for ( int i = 0 ; i < 3 ; i++) {
System.out.print(
( char )pushbackReader.read());
}
}
}
|
Output:
GEEKSFORGEEKS
XYZ
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(char%5B%5D)
3. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(char%5B%5D, int, int)
Last Updated :
05 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...