Java - LineNumberReader mark(int readAheadLimit) method



Description

The Java LineNumberReader mark(int readAheadLimit) method marks the current position in the stream so that it can be reset later using reset(). The parameter r specifies the read-ahead limit, which defines how many characters can be read before the mark becomes invalid.

Declaration

Following is the declaration for java.io.LineNumberReader.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

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of LineNumberReader mark(int readAheadLimit) method

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

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) throws IOException {
      FileReader fr = null;
      LineNumberReader lnr = null;
      int i;
      
      try {
         // create new reader
         fr = new FileReader("test.txt");
         lnr = new LineNumberReader(fr);
         
         // reads and prints data from reader
         System.out.println((char)lnr.read());
         System.out.println((char)lnr.read());
         
         // mark invoked at this position
         lnr.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)lnr.read());
         System.out.println((char)lnr.read());
         
         // if this reader supports mark() an reset()
         if(lnr.markSupported()) {
         
            // reset() repositioned the reader to the mark
            lnr.reset();
            System.out.println("reset() invoked");
            System.out.println((char)lnr.read());
            System.out.println((char)lnr.read());
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases system resources
         if(fr!=null)
            fr.close();
         if(lnr!=null)
            lnr.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

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 - Marking and Resetting a Line

The following example shows the usage of Java LineNumberReader mark(int readAheadLimit) method. This example demonstrates marking a position in the stream, reading some lines, and then using reset() to go back to the marked position.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "First Line\nSecond Line\nThird Line";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

      try {
         System.out.println("Reading first line: " + lnr.readLine());

         // Mark the current position
         lnr.mark(50);
         System.out.println("Marked at Line Number: " + lnr.getLineNumber());

         System.out.println("Reading second line: " + lnr.readLine());
         System.out.println("Reading third line: " + lnr.readLine());

         // Reset to the marked position
         lnr.reset();
         System.out.println("After reset, current line: " + lnr.readLine());
         System.out.println("Line Number after reset: " + lnr.getLineNumber());

         lnr.close();
      } 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−

Reading first line: First Line
Marked at Line Number: 1
Reading second line: Second Line
Reading third line: Third Line
After reset, current line: Second Line
Line Number after reset: 2

Explanation

  • Reads the first line (First Line).

  • Calls mark(50), saving the position at line 1.

  • Reads two more lines (Second Line and Third Line).

  • Calls reset(), which returns to the marked position.

  • Reads again from the marked line (Second Line), confirming that reset() worked.

Example - Marking and Skipping Lines

The following example shows the usage of Java LineNumberReader mark(int readAheadLimit) method. This example marks a position, skips a few lines, and then resets to the marked position.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "Apple\nBanana\nCherry\nDate\nElderberry";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

      try {
         System.out.println("Reading first line: " + lnr.readLine());

         // Mark the position after reading the first line
         lnr.mark(100);
         System.out.println("Marked at Line Number: " + lnr.getLineNumber());

         // Skip two lines
         lnr.readLine();
         lnr.readLine();
         System.out.println("After skipping, Line Number: " + lnr.getLineNumber());

         // Reset to the marked position
         lnr.reset();
         System.out.println("After reset, reading again: " + lnr.readLine());
         System.out.println("Line Number after reset: " + lnr.getLineNumber());

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

Output

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

Reading first line: Apple
Marked at Line Number: 1
After skipping, Line Number: 3
After reset, reading again: Banana
Line Number after reset: 2

Explanation

  • Reads the first line (Apple).

  • Calls mark(100), marking the position at line 1.

  • Reads two more lines (Banana and Cherry), moving to line 3.

  • Calls reset(), returning to line 1.

  • Reads again, confirming that resetting works as expected.

java_io_linenumberreader.htm
Advertisements