Java.io.CharArrayReader.reset() Method

Advertisements


Description

The java.io.CharArrayReader.reset() method resets the stream to the last marked position. If the stream is not marked, it resets to the begining of the stream.

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

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

package com.tutorialspoint;

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

public class CharArrayReadDemo {
   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();
      }
   }
}

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 



Advertisements
Advertisements