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:
// Java program to illustrate
// PushbackReader unread(int) method
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
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
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKSFORGEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
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 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:
// Java program to illustrate
// PushbackReader unread(char[]) method
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
0
; i < str.length(); i++) {
           Â
System.out.print(
               Â
(
char
)pushbackReader.read());
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Create character array
       Â
char
[] cbuf =
new
char
[] {
'A'
,
'B'
,
'C'
};
Â
ÂÂ Â Â Â Â Â Â Â
// Call unread() method
       Â
pushbackReader.unread(cbuf);
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println();
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
0
; i < cbuf.length; i++) {
           Â
System.out.print(
               Â
(
char
)pushbackReader.read());
       Â
}
   Â
}
}
Output:GEEKS ABC
Program 2:
// Java program to illustrate
// PushbackReader unread(char[]) method
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKSFORGEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
0
; i < str.length(); i++) {
           Â
System.out.print(
               Â
(
char
)pushbackReader.read());
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Create character array
       Â
char
[] cbuf =
new
char
[] {
'X'
,
'Y'
,
'Z'
};
Â
ÂÂ Â Â Â Â Â Â Â
// Call unread() method
       Â
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:
// Java program to illustrate
// PushbackReader
// unread(char[], int, int) method
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
0
; i < str.length(); i++) {
           Â
System.out.print(
               Â
(
char
)pushbackReader.read());
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Create character array
       Â
char
[] cbuf
           Â
=
new
char
[] {
'A'
,
'B'
,
'C'
,
                          Â
'D'
,
'E'
};
Â
ÂÂ Â Â Â Â Â Â Â
// Call unread() method
       Â
pushbackReader.unread(cbuf,
2
,
3
);
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println();
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
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
Â
Âimport
java.io.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
       Â
throws
IOException
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create string
       Â
String str =
"GEEKSFORGEEKS"
;
Â
ÂÂ Â Â Â Â Â Â Â
// Create stringReader
       Â
StringReader strReader
           Â
=
new
StringReader(str);
Â
ÂÂ Â Â Â Â Â Â Â
// Create object of
       Â
// PushbackReader
       Â
PushbackReader pushbackReader
           Â
=
new
PushbackReader(strReader,
100
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(
int
i =
0
; i < str.length(); i++) {
           Â
System.out.print(
               Â
(
char
)pushbackReader.read());
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Create character array
       Â
char
[] cbuf
           Â
=
new
char
[] {
'W'
,
'X'
,
                          Â
'Y'
,
'Z'
};
Â
ÂÂ Â Â Â Â Â Â Â
// Call unread() method
       Â
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)