Java BufferedInputStream reset() Method with Example

BufferedInputStream Class reset() method: Here, we are going to learn about the reset() method of BufferedInputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 01, 2020

BufferedInputStream Class reset() method

  • reset() method is available in java.io package.
  • reset() method is used to reset this BufferedInputStream or in other words, we can say it reset the stream position to the position where mark() method invoked last on stream.
  • reset() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • reset() method may throw an exception at the time of resetting this stream.
    IOException: This exception may throw while performing input/output operation.

Syntax:

    public void reset();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void reset() method of 
// BufferedInputStream

import java.io.*;

public class ResetBIS {
    public static void main(String[] args) throws Exception {
        // To open text file by using 
        // FileInputStream
        FileInputStream fis = new FileInputStream("e:/includehelp.txt");

        // By using BufferedInputStream() isto change
        // the stream fis into buff_str
        BufferedInputStream buff_str = new BufferedInputStream(fis);

        // By using available() method is to
        // return the no. of bytes to be left 
        // for reading
        Integer n_byte = buff_str.available();
        System.out.println("Left avail bytes = " + n_byte);

        // Read character from the stream
        char ch1 = (char) buff_str.read();
        char ch2 = (char) buff_str.read();
        char ch3 = (char) buff_str.read();

        System.out.println("ch1: " + ch1);
        System.out.println("ch2 : " + ch2);

        // By using mark() method isto
        // set the limit the number of byte
        // to be read 
        buff_str.mark(5);
        System.out.println("ch3: " + ch3);

        // It reset the stream buff_str
        // to the position where the mark()
        // was called last or most recent call
        buff_str.reset();

        // Read from the stread
        char ch4 = (char) buff_str.read();
        char ch5 = (char) buff_str.read();

        // Display character
        System.out.println("ch4: " + ch4);
        System.out.println("ch5: " + ch5);

        fis.close();
        buff_str.close();
    }
}

Output

Left avail bytes = 33
ch1: H
ch2 : e
ch3: l
ch4: l
ch5: o


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.