Java - FilterReader mark(int readAheadLimit) method



Description

The Java FilterReader mark(int readAheadLimit) method marks the current position in the input stream, allowing the stream to return to this position later using reset(). readLimit specifies the maximum number of bytes that can be read before the mark becomes invalid.

Declaration

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

public void mark(int readAheadLimit)

Parameters

readAheadLimit − Limit on the number of characters that may be read while still preserving the mark.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of FilterReader mark(int readAheadLimit) method

The following example shows the usage of Java FilterReader mark(int readAheadLimit) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;      
      
      try {
         // create new reader
         r = new StringReader("ABCDEF");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // reads and prints FilterReader
         System.out.println((char)fr.read());
         System.out.println((char)fr.read());
                  
         // mark invoked at this position
         fr.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)fr.read());
         System.out.println((char)fr.read());
                  
         // reset() repositioned the stream to the mark
         fr.reset();
         System.out.println("reset() invoked");
         System.out.println((char)fr.read());
         System.out.println((char)fr.read());
         
      } catch(IOException e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output

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

A
B
mark() invoked
C
D
reset() invoked
C
D
Stream is closed system resources released

Example - Using mark(int readLimit) with BufferedReader

The following example shows the usage of Java FilterReader mark(int readAheadLimit) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         System.out.println("Mark supported? " + fr.markSupported());

         // Read and print first character
         System.out.print((char) fr.read());

         // Mark the current position
         fr.mark(5); // Can read up to 5 characters before mark expires

         // Read next two characters
         System.out.print((char) fr.read());
         System.out.print((char) fr.read());

         // Reset back to the marked position
         fr.reset();

         // Read again from the marked position
         System.out.print((char) fr.read());
         System.out.print((char) fr.read());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Mark supported? true
Helo
elo

Explanation

  • Uses BufferedReader, which supports marking.

  • Marks the position after reading one character.

  • Reads two more characters.

  • Resets to the marked position.

  • Reads again from the marked position.

Example - Handling mark() in PushbackReader

The following example shows the usage of Java FilterReader mark(int readAheadLimit) method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (FilterReader fr = new PushbackReader(new FileReader("example.txt"))) {
         System.out.println("Mark supported? " + fr.markSupported());

         // Attempting to mark
         fr.mark(10);
         System.out.print((char) fr.read());

         // Resetting to the marked position
         fr.reset();
         System.out.print((char) fr.read());

      } catch (IOException e) {
         System.out.println("Reset failed: " + e.getMessage());
      }
   }
}

Output(if example.txt contains "Java")

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

Mark supported? false
Reset failed: mark/reset not supported

Explanation

  • Uses PushbackReader, which is not supporting marking.

  • Marking the position before reading a character causes IOException

  • Exception is printed.

java_io_filterreader.htm