Java.io.CharArrayReader.mark() Method



Description

The java.io.CharArrayReader.mark(int readAheadLimit) method marks the current position in the stream. Invoking reset() will reposition the stream to this point.

Declaration

Following is the declaration for java.io.CharArrayReader.mark(int readAheadLimit) method −

public void mark(int readAheadLimit)

Parameters

readAheadLimit − The parameter sets the limit on the number of characters that can be read while preserving the mark. The argument is normally ignored due to the fact that there is no actual limit as the stream's input comes from character array.

Return Value

The method doesn't return any value.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.CharArrayReader.mark(int readAheadLimit) method.

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);
         
         // read and print the characters from the stream
         System.out.println(car.read());
         System.out.println(car.read());
         
         // mark() is invoked at this position
         car.mark(0);
         System.out.println("Mark() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         
         // reset() is invoked at this position
         car.reset();
         System.out.println("Reset() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         System.out.println(car.read());
         
      } catch(IOException e) {
         // if I/O error occurs
         System.out.print("Stream is already closed");
      } 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 −

65
66
Mark() is invoked
67
68
Reset() is invoked
67
68
69
java_io_chararrayreader.htm
Advertisements