PushbackInputStream reset() method in Java with Examples
The reset() method of PushbackInputStream class in Java is used to reset the steam to the position where mark() method was called. This method does nothing for PushbackInputStream. Syntax:
public void reset()
throws IOException
Overrides: This method overrides the reset() method of FilterInputStream class. Parameters: This method does not accept any parameter. Return value: This method does not return any value. Exceptions: This method throws IOException whenever this method is called. Below programs illustrate reset() method of PushbackInputStream class in IO package: Program 1:
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] byteArray
= new byte [] { 'G' , 'E' , 'E' ,
'K' , 'S' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
for ( int i = 0 ; i < byteArray.length; i++) {
System.out.print(
( char )pushbackInputStr.read());
}
pushbackInputStr.reset();
}
}
|
Output:
Exception in thread “main” java.io.IOException: mark/reset not supported GEEKS
Program 2:
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] byteArray
= new byte [] { 'H' , 'E' , 'L' ,
'L' , 'O' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
pushbackInputStr.reset();
for ( int i = 0 ; i < byteArray.length; i++) {
System.out.print(
( char )pushbackInputStr.read());
}
}
}
|
Last Updated :
22 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...