BufferedInputStream reset() method in Java with Examples Last Updated : 05 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. General Contract: 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. Syntax: 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". Java // Java program to illustrate // BufferedInputStream reset() method import java.io.*; public class GFG { public static void main(String[] args) { // Create input stream 'demo.txt' // for reading containing text "GEEKS" FileInputStream inputStream = new FileInputStream( "c:/demo.txt"); // Convert inputStream to // bufferedInputStream BufferedInputStream buffInputStr = new BufferedInputStream( inputStream); // Read and print characters one by one System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); // Mark is set on the input stream buffInputStr.mark(0); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "reset() called"); // Reset() is invoked buffInputStr.reset(); // Read and print characters System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); } } Input: Output: Program 2: Assume the existence of file "c:/demo.txt". Java // Java program to illustrate // BufferedInputStream reset() method import java.io.*; public class GFG { public static void main(String[] args) { // Create input stream 'demo.txt' // for reading containing text // "GEEKSFORGEEKS" FileInputStream inputStream = new FileInputStream( "c:/demo.txt"); // Convert inputStream to // bufferedInputStream BufferedInputStream buffInputStr = new BufferedInputStream( inputStream); // Read and print characters one by one System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); // Mark is set on the input stream buffInputStr.mark(0); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "reset() called"); // Reset() is invoked buffInputStr.reset(); // Read and print characters System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); System.out.println( "Char : " + (char)buffInputStr.read()); } } Input: Output: References: https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#reset() Comment More infoAdvertise with us Next Article BufferedInputStream skip(long) method in Java with Examples P pp_pankaj Follow Improve Article Tags : Java Java-Functions Java-IO package Practice Tags : Java Similar Reads BufferedInputStream read() method in Java with Examples read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time. Syntax: public int read() Overrides: It overrides read() me 3 min read ByteArrayInputStream reset() method in Java with Examples The reset() method is a built-in method of the Java.io.ByteArrayInputStream is invoked by mark() method. It repositions the input stream to the marked position. Syntax: public void reset() Parameters: The function does not accepts any parameter. Return Value: The function does not returns anything. 2 min read BufferedReader reset() method in Java with Examples The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Syntax: public void reset() throws IOException Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accep 3 min read BufferedInputStream skip(long) method in Java with Examples The skip(long) method of BufferedInputStream class in Java is used to skip n bytes of data from the buffered input stream. The number of bytes skipped is stored and returned as long type. The termination condition involves either of the two: Either reading into a byte array until n-bytes are covered 2 min read BufferedInputStream close() method in Java with Examples The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch 2 min read BufferedOutputStream write() method in Java with Examples The write(int) method of BufferedOutputStream class in Java is used to write the specified byte to the buffered output stream. The specified byte is passed as an integer to the write() method here. It is used to write one byte as a time to the BufferedOutputStream. Syntax: public void write(int b) t 3 min read Like