Home » Java programming language

Java CharArrayReader mark() Method with Example

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

CharArrayReader Class mark() method

  • mark() method is available in java.io package.
  • mark() method is used to mark the current position in the stream and whenever the call to reset() method so it reset the stream to the position set by the most recent call of mark() method.
  • mark() 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.
  • mark() method may throw an exception at the time of marking the stream.
    IOException: This exception may throw when the given parameter is not valid.

Syntax:

    public void mark(int r_limit);

Parameter(s):

  • int r_limit – represents the limit on the number of character that can be read while preserving the mark.

Return value:

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

Example:

// Java program to demonstrate the example 
// of void mark(int r_limit) method of 
// CharArrayReader

import java.io.*;

public class MarkOfCAR {
 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);

   // By using read() method isto 
   // read the character from car_stm
   int i1 = car_stm.read();
   int i2 = car_stm.read();
   int i3 = car_stm.read();
   int i4 = car_stm.read();

   System.out.println("i1: " + i1);

   // By using mark() method isto
   // set the current position in this
   // car_stm
   System.out.println("car_stm.mark(0): ");
   car_stm.mark(0);
   System.out.println("i2: " + i2);
   System.out.println("i3: " + i3);

   // By using reset() method isto
   // reset the stream to the position 
   // set by the call mark() method
   System.out.println("car_stm.reset(): ");
   car_stm.reset();
   System.out.println("i2: " + i2);
   System.out.println("i3: " + i3);
   System.out.println("i4: " + i4);


  } catch (IOException e) {
   System.out.print("Stream closed!!!!");
  } finally {

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

Output

i1: 97
car_stm.mark(0): 
i2: 98
i3: 99
car_stm.reset(): 
i2: 98
i3: 99
i4: 100



Comments and Discussions!

Load comments ↻






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