BufferedReader mark() method in Java with Examples
The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so that same byte can be read again.
public void mark(int readAheadLimit) throws IOException
Overrides: It overrides the mark() method of Reader class.
Parameters: This method accepts readAheadLimit of Integer type which 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 can throw two types of exceptions.
- IllegalArgumentException – This exception is thrown if the passed parameter readAheadLimit is less than zero.
- IOException – This exception is thrown if an I/O error occurs.
Below programs illustrate mark() method in BufferedReader class in IO package.
Program 1: Assume the existence of the file “c:/demo.txt”.
Char : G Char : E Char : E Char : K Char : K Char : S
Program 2: Assume the existence of the file “c:/demo.txt”.
Char : G Char : E Char : E Char : K Char : S Char : S Char : F Char : O Char : R
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.