PushbackInputStream mark() method in Java with Examples
Last Updated :
16 Jun, 2022
The mark() method of PushbackInputStream class in Java is used to mark the current position in the input stream. This method does nothing for PushbackInputStream.
Syntax:
public void mark(int readlimit)
Overrides: This method overrides the mark() method of FilterInputStream class.
Parameters: This method accepts single parameter readlimit that represents the maximum limit of bytes that can be read before the mark position becomes invalid.
Return value: This method does not return any value.
Exceptions: This method does not throw any exception.
Below programs illustrate mark() 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.mark( 5 );
}
}
|
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.mark( 1 );
for ( int i = 0 ; i < byteArray.length; i++) {
System.out.print(
( char )pushbackInputStr.read());
}
}
}
|
References: https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#mark(int)
Share your thoughts in the comments
Please Login to comment...