Reader reset() method in Java with Examples - GeeksforGeeks

13 Feb, 2019
The reset() method of Reader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting.
Syntax:
public void reset()
Parameters: This method do not accepts any parameter.
Return Value: This method do not returns any value.
Exception: This method throws IOException:
if some error occurs while input output
if the stream has not been marked
if the mark has been invalidated
if the reset() method is not supported.
Below methods illustrates the working of reset() method:
Program 1:
// Java program to demonstrate// Reader reset() method  import java.io.*;import java.util.*;  class GFG {    public static void main(String[] args)    {          try {              String str = "GeeksForGeeks";              // Create a Reader instance            Reader reader                = new StringReader(str);              // Get the character            // to be read from the stream            int ch;              // Read the first 10 characters            // to this reader using read() method            // This will put the str in the stream            // till it is read by the reader            for (int i = 0; i < 10; i++) {                ch = reader.read();                System.out.print((char)ch);            }              System.out.println();              // mark the stream for            // 5 characters using reset()            reader.mark(5);              // reset the stream position            reader.reset();              // Read the 5 characters from marked position            // to this reader using read() method            for (int i = 0; i < 5; i++) {                ch = reader.read();                System.out.print((char)ch);            }        }        catch (Exception e) {            System.out.println(e);        }    }}
GeeksForGe
eks??

Program 2:
// Java program to demonstrate// Reader reset() method  import java.io.*;import java.util.*;  class GFG {    public static void main(String[] args)    {          try {            String str = "GeeksForGeeks";              // Create a Reader instance            Reader reader                = new StringReader(str);              // Get the character            // to be read from the stream            int ch;              // Read the first 10 characters            // to this reader using read() method            // This will put the str in the stream            // till it is read by the reader            for (int i = 0; i < 10; i++) {                ch = reader.read();                System.out.print((char)ch);            }              System.out.println();              // reset the stream position            reader.reset();              // Read the 5 characters from marked position            // to this reader using read() method            for (int i = 0; i < 5; i++) {                ch = reader.read();                System.out.print((char)ch);            }        }        catch (Exception e) {            System.out.println(e);        }    }}
GeeksForGe
Geeks

Reference: https://docs.oracle.com/javase/9/docs/api/java/io/Reader.html#reset–
Java-Functions
Java-IO package
Java-Reader
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java