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 IOExceptionParameters: 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:
// Java program to illustrate// PushbackReader unread(int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Call unread() method       ÂpushbackReader.unread(65);       ÂSystem.out.print(           Â"\n"           Â+ (char)pushbackReader.read());   Â}}Output:GEEKS A
Program 2:
// Java program to illustrate// PushbackReader unread(int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKSFORGEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Call unread() method       ÂpushbackReader.unread('Z');       ÂSystem.out.print(           Â"\n"           Â+ (char)pushbackReader.read());   Â}}Output:GEEKSFORGEEKS Z
- 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 IOExceptionParameters: 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:
// Java program to illustrate// PushbackReader unread(char[]) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Create character array       Âchar[] cbuf =newchar[] {'A','B','C'};       Â// Call unread() method       ÂpushbackReader.unread(cbuf);       ÂSystem.out.println();       Âfor(inti =0; i < cbuf.length; i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}   Â}}Output:GEEKS ABC
Program 2:
// Java program to illustrate// PushbackReader unread(char[]) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKSFORGEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Create character array       Âchar[] cbuf =newchar[] {'X','Y','Z'};       Â// Call unread() method       ÂpushbackReader.unread(cbuf);       ÂSystem.out.println();       Âfor(inti =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 IOExceptionParameters: 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:
// Java program to illustrate// PushbackReader// unread(char[], int, int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Create character array       Âchar[] cbuf           Â=newchar[] {'A','B','C',                          Â'D','E'};       Â// Call unread() method       ÂpushbackReader.unread(cbuf,2,3);       ÂSystem.out.println();       Âfor(inti =0; i <3; i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}   Â}}Output:GEEKS CDE
Program 2:
// Java program to illustrate// PushbackReader// unread(char[], int, int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create string       ÂString str ="GEEKSFORGEEKS";       Â// Create stringReader       ÂStringReader strReader           Â=newStringReader(str);       Â// Create object of       Â// PushbackReader       ÂPushbackReader pushbackReader           Â=newPushbackReader(strReader,100);       Âfor(inti =0; i < str.length(); i++) {           ÂSystem.out.print(               Â(char)pushbackReader.read());       Â}       Â// Create character array       Âchar[] cbuf           Â=newchar[] {'W','X',                          Â'Y','Z'};       Â// Call unread() method       ÂpushbackReader.unread(cbuf,1,3);       ÂSystem.out.println();       Âfor(inti =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)



