Java - Reader reset() method



Description

The Java Reader reset() method resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point.

Declaration

Following is the declaration for java.io.Reader.reset() method.

public void reset()

Parameters

NA

Return Value

This method does not return a value.

Exception

  • IOException − If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurs.

Example - Usage of Reader reset() method

The following example shows the usage of Reader reset() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class ReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // mark current position for maximum of 10 characters
         reader.mark(10);

         // read five more chars
         for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // reset back to the marked position
         reader.reset();

         // change line
         System.out.println();

         // read six more chars
         for (int i = 0; i < 6; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // close the stream
         reader.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello World
 World

Example - Resetting after reading part of a string

The following example shows the usage of Reader reset() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.StringReader;
import java.io.IOException;

public class ReaderDemo {
   public static void main(String[] args) {
      try (StringReader reader = new StringReader("ABCDE")) {
         if (reader.markSupported()) {
            reader.mark(100);  // Mark the current position (before reading)
            System.out.print((char) reader.read()); // Read 'A'
            System.out.print((char) reader.read()); // Read 'B'

            reader.reset(); // Reset to the marked position

            System.out.print((char) reader.read()); // Read 'A' again
            System.out.print((char) reader.read()); // Read 'B' again
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

ABAB

Explanation

  • The reader reads two characters (A, B), then reset() moves the cursor back to the beginning.

  • The same characters are read again after reset.

Example - Using BufferedReader with mark() and reset()

The following example shows the usage of Reader reset() method.

ReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class ReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader reader = new BufferedReader(new StringReader("Hello, World!"))) {
         if (reader.markSupported()) {
            reader.mark(50);  // Mark current position
            char[] firstRead = new char[5];
            reader.read(firstRead);
            System.out.println("First read: " + new String(firstRead)); // "Hello"

            reader.reset(); // Go back to marked position
            char[] secondRead = new char[5];
            reader.read(secondRead);
            System.out.println("After reset: " + new String(secondRead)); // "Hello" again
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

First read: Hello
After reset: Hello

Explanation

  • Reads 5 characters after marking, then resets.

  • Reads the same 5 characters again after reset.

java_io_reader.htm
Advertisements