Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - CharArrayReader reset() method



Description

The Java CharArrayReader reset() method moves the read position back to the mark set by mark(int readAheadLimit). If mark() was not called before reset(), it resets the reader to the beginning of the character array.

Declaration

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

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If any I/O error occurs.

Example - Using CharArrayReader reset() method

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

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {      
      CharArrayReader car = null;
      char[] ch = {'A', 'B', 'C', 'D', 'E'};
      
      try {
         // create new character array reader
         car = new CharArrayReader(ch);
         
         int value = 0;
         
         // read till the end of the stream
         while((value = car.read())!=-1) {
            
            // convert integer to char
            char c = (char)value;
            
            // print characters
            System.out.print(c + " ");
         }
         
         // reset invoked
         car.reset();
         System.out.println("\nReset() invoked");
         
         // read till the end of the stream
         while((value = car.read())!=-1) {
            
            // convert integer to char
            char c = (char)value;
            
            // print characters
            System.out.print(c + " ");
         }
         
      } catch(IOException e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(car!=null)
            car.close();
      }
   }
}

Output

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

A B C D E 
Reset() invoked
A B C D E 

Example - Resetting to the Beginning (No Mark Used)

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

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Hello, World!".toCharArray();

      try (CharArrayReader reader = new CharArrayReader(data)) {
         // Read and print first 5 characters
         for (int i = 0; i < 5; i++) {
            System.out.print((char) reader.read());
         }

         // Reset to the beginning
         reader.reset();
         System.out.println("\nAfter reset:");

         // Read again from the beginning
         int charData;
         while ((charData = reader.read()) != -1) {
            System.out.print((char) charData);
         }

      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

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

Hello
After reset:
Hello, World!

Explanation

  • The string "Hello, World!" is converted into a character array and passed to CharArrayReader.

  • The first 5 characters ("Hello") are read and printed.

  • reset() is called, which moves the read position back to the beginning.

  • The entire string is read again from the start.

Example - Using mark() and reset() to Return to a Specific Position

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

CharArrayReaderDemo.java

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {
      char[] data = "Java Programming".toCharArray();

      try (CharArrayReader reader = new CharArrayReader(data)) {
         // Read and print first 5 characters
         for (int i = 0; i < 5; i++) {
            System.out.print((char) reader.read());
         }

         // Mark the current position
         reader.mark(0);
         System.out.println("\nMark set after reading 'Java '.");

         // Read the next 6 characters
         for (int i = 0; i < 6; i++) {
            System.out.print((char) reader.read());
         }

         // Reset back to the marked position
         reader.reset();
         System.out.println("\nAfter reset:");

         // Read again from the marked position
         int charData;
         while ((charData = reader.read()) != -1) {
            System.out.print((char) charData);
         }

      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

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

Java 
Mark set after reading 'Java '.
Progra
After reset:
Programming

Explanation

  • First 5 characters ("Java ") are read.

  • mark(0) is called, setting a checkpoint at this position.

  • Next 6 characters ("Progra") are read.

  • reset() moves back to the marked position ("Java ").

  • Reading resumes from the marked position ("Programming") and continues to the end.

Key Takeaways

  • Non-Blocking Checkready() returns true if characters can be read without blocking. Useful before reading large data to prevent unnecessary waiting.

  • Usage with read()− Works well in loops to ensure smooth reading.

  • Doesn't Consume Characters− Unlike read(), calling ready() does not move the read position.

  • Interaction with skip()ready() remains true if there are characters left after skipping.

These examples show how reset() can rewind reading positions efficiently in CharArrayReader!

java_io_chararrayreader.htm
Advertisements