Home » Java programming language

Java StringBufferInputStream reset() Method with Example

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

StringBufferInputStream Class reset() method

  • reset() method is available in java.io package.
  • reset() method is used to reset this StringBufferInputStream and it resets the stream to the position set by mark() method most recently.
  • 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 does not throw an exception at the time of resetting the stream.

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 StringBufferInputStream

import java.io.*;

public class ResetOfSBIS {
    public static void main(String[] args) throws Exception {
        StringBufferInputStream is_stm = null;
        int count = 0;

        try {
            // Instantiates StringBufferInputStream
            is_stm = new StringBufferInputStream("Java Programming");

            // Loop to read until available
            // bytes left
            while ((count = is_stm.read()) != -1) {
                // Display corresponding char value
                char ch = (char) count;

                // Display value of ch
                System.out.println("ch: " + ch);

                // By using skip() method is to
                // skip 1 byte of data from the is_stm
                // stream
                long skip = is_stm.skip(3);
                System.out.println("is_stm.skip(1): " + skip);
            }

            // By using reset() method is to reset
            // the stream
            System.out.println("is_stm.reset(): ");
            is_stm.reset();

            while ((count = is_stm.read()) != -1) {
                // Display corresponding char value
                char ch = (char) count;

                // Display value of ch
                System.out.println("ch: " + ch);
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        } finally {
            // with the help of this block is to
            // free all necessary resources linked
            // with the stream
            if (is_stm != null) {
                is_stm.close();
            }
        }
    }
}

Output

ch: J
is_stm.skip(1): 3
ch:  
is_stm.skip(1): 3
ch: g
is_stm.skip(1): 3
ch: m
is_stm.skip(1): 3
is_stm.reset(): 
ch: J
ch: a
ch: v
ch: a
ch:  
ch: P
ch: r
ch: o
ch: g
ch: r
ch: a
ch: m
ch: m
ch: i
ch: n
ch: g



Comments and Discussions!

Load comments ↻






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