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 accept any parameter.

Return value: The method does not return any value.



Exceptions: The method throws IOException if the mark() method is never called on the stream or if the value of mark is invalid.

Below programs illustrate reset() method in BufferedReader class in IO package:

Program 1: Assume the existence of the file “c:/demo.txt”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// BufferedReader reset() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // for containing text "GEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        // Read and print characters
        // one by one
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Mark is set on the stream
        buffReader.mark(0);
  
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Reset() is invoked
        buffReader.reset();
  
        // Read and print characters
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
    }
}

chevron_right


Output:

Char : G
Char : E
Char : E
Char : K
Char : K
Char : S

Program 2: Assume the existence of the file “c:/demo.txt”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// BufferedReader reset() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // containing text "GEEKSFORGEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        // Read and print characters
        // one by one
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Mark is set on the stream
        buffReader.mark(0);
  
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Reset() is invoked
        buffReader.reset();
  
        // read and print characters
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
    }
}

chevron_right


Output:

Char : G
Char : E
Char : E
Char : K
Char : S
Char : S
Char : F
Char : O
Char : R

References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#reset()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.