Java - BufferedReader mark() method



Description

The Java BufferedReader mark(int) method marks the current position in the stream. Invoking reset() will reposition the stream to this point.

This method is used to mark a position in the stream so that you can reset the reader to that position later. The readAheadLimit specifies the maximum number of characters that can be read before the mark becomes invalid.

Declaration

Following is the declaration for java.io.BufferedReader.mark() method.

public void mark(int readAheadLimit)

Parameters

readAheadLimit − number of characters to be read while preserving the mark.

Return Value

The method doesn't return any value.

Exception

  • IOException − If an I/O error occurs.

  • IllegalArgumentException − If readAheadLimit is < 0.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDE 

Example - Using mark() method

The following example shows the usage of Java BufferedReader mark() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      InputStreamReader isr = null;
      BufferedReader br = null;

      try {
         // open input stream example.txt for reading purpose.
         is = new FileInputStream("example.txt");
         
         // create new input stream reader
         isr = new InputStreamReader(is);
         
         // create new buffered reader
         br = new BufferedReader(isr);

         // reads and prints BufferedReader
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
         // mark invoked at this position
         br.mark(26);
         System.out.println("mark() invoked");
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
         // reset() repositioned the stream to the mark
         br.reset();
         System.out.println("reset() invoked");
         System.out.println((char)br.read());
         System.out.println((char)br.read());
         
      } catch (Exception e) {
         // exception occurred.
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(isr!=null)
            isr.close();
         if(br!=null)
            br.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

Example - Using mark() and reset() for Re-reading a Portion of the Input

The following example shows the usage of Java BufferedReader mark() and reset() methods.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      // Input string
      String input = "Hello, World!\nThis is a BufferedReader example.\nHave a nice day!";

      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         // Read the first line
         System.out.println(reader.readLine());

         // Mark the current position in the stream
         reader.mark(100); // Allows reading up to 100 characters before the mark is invalid

         // Read the next line
         System.out.println(reader.readLine());

         // Reset the reader to the marked position
         reader.reset();

         // Re-read the line after reset
         System.out.println(reader.readLine());
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

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

Hello, World!
This is a BufferedReader example.
This is a BufferedReader example.

Explanation

  • A BufferedReader is initialized with a StringReader that wraps the input string.

  • The first line is read using readLine() method.

  • The mark(100) method marks the current position, allowing up to 100 characters to be read before the mark becomes invalid.

  • After reading another line, the reset() method is called to return the reader to the marked position.

  • The previously read line is re-read after the reset.

Example - Using mark() to Skip and Revisit a Portion of the Input

The following example shows the usage of Java BufferedReader mark() method.

BufferedReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) {
      // Input string
      String input = "Line 1: Introduction\nLine 2: Details\nLine 3: Conclusion";

      try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
         // Mark the starting position
         reader.mark(50); // Allows reading up to 50 characters before the mark is invalid

         // Skip the first line
         reader.readLine();

         // Read the second line
         System.out.println("Current Line: " + reader.readLine());

         // Reset to the marked position
         reader.reset();

         // Read from the start again
         System.out.println("Revisiting Line: " + reader.readLine());
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

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

Current Line: Line 2: Details
Revisiting Line: Line 1: Introduction

Explanation

  • A BufferedReader is initialized with a StringReader wrapping the input string.

  • The mark(50) method marks the beginning of the stream, allowing up to 50 characters to be read before the mark is invalid.

  • The first line is skipped using readLine() method.

  • The second line is read and printed.

  • The reset() method repositions the reader to the marked position, allowing the first line to be read again.

Notes on mark()

  • readAheadLimit− The number of characters that can be read before the mark is invalid. Exceeding this limit causes the mark to be lost.

  • Support for mark()− Always check BufferedReader's markSupported() method before using mark() as some readers may not support marking.

All examples illustrate how mark() can be used to revisit earlier or later parts of the input efficiently.

java_io_bufferedreader.htm
Advertisements