BufferedReader reset() method in Java with Examples - GeeksforGeeks

05 Jun, 2020
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());    }}
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() methodimport 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());    }}
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()
Java-Functions
Java-IO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java