Home » Java programming language

Java CharArrayReader reset() Method with Example

CharArrayReader Class reset() method: Here, we are going to learn about the reset() method of CharArrayReader Class with its syntax and example.
Submitted by Preeti Jain, on March 27, 2020

CharArrayReader Class reset() method

  • reset() method is available in java.io package.
  • reset() method is used to reset this stream to the position set by the most recent call of mak() method when it exists otherwise it reset the stream to the starting position of the stream.
  • reset() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • reset() method may throw an exception at the time of resetting the stream.
    IOException: This exception may throw while getting any input/output error.

Syntax:

    public void reset();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void reset() method of CharArrayReader

import java.io.*;

public class ResetOfCAR {
 public static void main(String[] args) {
  CharArrayReader car_stm = null;
  char[] c_arr = {
   'a',
   'b',
   'c',
   'd'
  };

  try {
   // Instantiates CharArrayReader
   car_stm = new CharArrayReader(c_arr);
   int val = 0;

   while ((val = car_stm.read()) != -1) {
    char ch = (char) val;
    System.out.print(ch + " ");
   }

   System.out.println(" ");

   // By using reset() method isto reset
   // this car_stm stream
   System.out.println("car_stm.reset(): ");
   car_stm.reset();

   while ((val = car_stm.read()) != -1) {
    char ch = (char) val;
    System.out.print(ch + " ");
   }

  } catch (Exception ex) {
   System.out.print("Stream Not Reset!!!!");
  } finally {

   // Free all system resources linked
   // with the stream after closing
   // the stream
   if (car_stm != null)
    car_stm.close();
  }
 }
}

Output

a b c d  
car_stm.reset(): 
a b c d 



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.