I would like to know what are the mark()
and reset()
methods of BufferedReader
? How do I use them? I read the Javadoc but as a beginner I was unable to understand it.
The mark() method of BufferedInputStream class in Java is used to mark the current position in the input stream. Reset() method of the same class BufferedInputStream is called after the mark() method. Reset() fixes the position at the last marked position so that same byte can be read again.
The java. io. BufferedReader. reset() method resets the stream to the most recent mark.
The mark() method of Reader Class in Java is used to mark the stream as the checkpoint from where the stream read will start, once reset() is called. This method is not supported by all subclasses of Reader class. Syntax: public void mark(int readAheadLimit)
The mark
and reset
methods of streams provide a way to jump backwards in the stream and re-read data.
When you call mark()
on a BufferedReader
it will begin keeping data you read from that point forwards in its internal buffer. When you call reset()
it will jump back to the marked position of the stream, so the next read()
s will be satisfied by the in-memory buffer. When you read past the end of that buffer, then it will seamlessly go back to reading fresh data. BufferedInputStream
works the same way.
The int parameter to mark
tells it the maximum number of characters (for BufferedReader
) or bytes (for BufferedInputStream
) that you want to be able to go backwards. If you read too much data past the marked position, then the mark can be "invalidated", and calling reset()
will fail with an exception.
A little example:
BufferedReader r = new BufferedReader(new StringReader( "Happy Birthday to You!\n" + "Happy Birthday, dear " + System.getProperty("user.name") + "!")); r.mark(1000); // save the data we are about to read System.out.println(r.readLine()); // read the first line r.reset(); // jump back to the marked position r.mark(1000); // start saving the data again System.out.println(r.readLine()); // read the first line again System.out.println(r.readLine()); // read the second line r.reset(); // jump back to the marked position System.out.println(r.readLine()); // read the first line one final time
In that example, I wrapped the StringReader
in a BufferedReader
to get the readLine()
method, but StringReader
s already support mark
and reset
on their own! Streams that read from an in-memory data source usually support mark
and reset
themselves, because they already have all the data in memory so it is easy for them to read it again. Streams that read from files or pipes or network sockets do not naturally support mark
and reset
, but you can always add that feature to any stream by wrapping it in a BufferedInputStream
or BufferedReader
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With