BufferedInputStream reset() method in Java with Examples
The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class.
There are two cases:
- If mark() and reset() are supported i.e., if markSupported returns true,
- An IOException might be thrown if the mark() is not called since the creation of the input stream or the bytes that are read from the input stream since the last call of mark() method is greater than the argument to mark method at the last call.
- In case an IOException is not thrown, then the input stream is reset to a state such that all the bytes that are read after the most recent call of mark() is supplied again to subsequent callers of the read(). This is followed by the bytes that would have at reset() call.
- If mark() and reset() are not supported i.e., if markSupported returns false,
- Calling reset() method might throw an IOException.
- In case an IOException is not thrown, then input stream is reset to a fixed state that depends on the particular type of the input stream. The supplied bytes to subsequent callers of the read() depend on the particular type of the input stream.
public void reset()
Overrides: The method overrides the reset class in FilterInputStream.
Parameters: This method does not accept any parameter.
Return value: This method does not return any value.
Exception: This method throws IOException if this stream has not been marked or mark has been invalidated or input stream has been closed by invoking the close() method or an I/O error occurs.
Below programs illustrates reset() method in BufferedInputStream class in IO package:
Program 1: Assume the existence of file “c:/demo.txt”.
Program 2: Assume the existence of file “c:/demo.txt”.
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.