Open In App

BufferedReader reset() method in Java with Examples

Last Updated : 05 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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”.




// 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());
    }
}


Output:

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

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




// 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());
    }
}


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()



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads