PushbackReader mark(int) method in Java with Examples
Last Updated :
29 Apr, 2019
The mark() method of PushbackReader Class in Java is used to marks the current position of the PushbackReader. In the case of PushbackReader, this method always throws an exception as this method isn’t supported by the PushbackReader.
Syntax:
public void mark(int readAheadLimit)
Parameters: This method accepts a mandatory parameter readAheadLimit which is the limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
Return Value: This method do not returns any value.
Exception: This method throws IOException always as the mark() method is not supported.
Below methods illustrates the working of mark() method:
Program 1:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String s = "GeeksForGeeks" ;
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
pushbackReader.mark( 5 );
pushbackReader.close();
System.out.println( "Stream Closed." );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
java.io.IOException: mark/reset not supported
Program 2:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
try {
String s = "GFG" ;
StringReader stringReader
= new StringReader(s);
PushbackReader pushbackReader
= new PushbackReader(stringReader);
pushbackReader.mark( 1 );
pushbackReader.close();
System.out.println( "Stream Closed." );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
java.io.IOException: mark/reset not supported
Reference: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#mark-int-
Share your thoughts in the comments
Please Login to comment...