Java - LineNumberInputStream mark(int readlimit) method



Description

The Java LineNumberInputStream mark(int readlimit) method marks the current position in the input stream, so the stream can be reset back to this position using reset(). The readlimit parameter specifies how many bytes can be read before the mark becomes invalid.

Declaration

Following is the declaration for java.io.LineNumberInputStream.mark(int readlimit) method −

public void mark(int readlimit)

Parameters

readlimit − The maximum limit of bytes that can be read before the mark position becomes invalid.

Return Value

The method does not return any value.

Exception

NA

Example - Usage of LineNumberInputStream mark(int readlimit) method

The following example shows the usage of Java LineNumberInputStream mark(int readlimit) method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      int i,j;
      char c;
      
      try {
         // create new input streams
         fis = new FileInputStream("test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // reads and prints data from input stream
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         
         // mark invoked at this position
         lnis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         
         // if this input stream supports mark() an reset()
         if(lnis.markSupported()) {
         
            // reset() repositioned the stream to the mark
            lnis.reset();
            System.out.println("reset() invoked");
            System.out.println((char)lnis.read());
            System.out.println((char)lnis.read());
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.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

Example - Using mark(int readlimit) to Re-read Data

The following example shows the usage of Java LineNumberInputStream mark(int readlimit) method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lnInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + lnInputStream.markSupported());

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

         // Mark the current position
         lnInputStream.mark(5); // Allows reading up to 5 bytes before mark expires

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

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

         // Read again from the marked position
         System.out.print((char) lnInputStream.read());
         System.out.print((char) lnInputStream.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? false
A
Bjava.io.IOException: mark/reset not supported
	at java.base/java.io.InputStream.reset(InputStream.java:741)
	at java.base/java.io.LineNumberInputStream.reset(LineNumberInputStream.java:289)
	at com.tutorialspoint.LineNumberInputStreamDemo.main(LineNumberInputStreamDemo.java:23)

Explanation

  • Uses LineNumberInputStream, which is not supporting marking.

  • Calls markSupported() to check if marking is available.

  • Marks a position after reading one character.

  • Reads two more characters, then resets to the marked position. As reset() is not supported, an exception occurs.

Example - Using mark() with getLineNumber()

The following example shows the usage of Java LineNumberInputStream mark(int readlimit) method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lnInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Marking at line: " + lnInputStream.getLineNumber());

         lnInputStream.mark(10); // Mark the current position

         int data;
         while ((data = lnInputStream.read()) != -1) {
            if (data == '\n') {
               System.out.println("Line Number: " + lnInputStream.getLineNumber());
               break; // Stop after reading one line
            }
         }

         // Reset back to the marked position
         lnInputStream.reset();
         System.out.println("After reset, Line Number: " + lnInputStream.getLineNumber());

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

Output(if example.txt contains multiple lines.)

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

Marking at line: 0
Line Number: 1
java.io.IOException: mark/reset not supported
	at java.base/java.io.InputStream.reset(InputStream.java:741)
	at java.base/java.io.LineNumberInputStream.reset(LineNumberInputStream.java:289)
	at com.tutorialspoint.LineNumberInputStreamDemo.main(LineNumberInputStreamDemo.java:23)

Explanation

  • Uses LineNumberInputStream to read a file.

  • Calls mark(10) to mark the current position.

  • Reads one full line while tracking the line number.

  • Trying to reset using reset() method, it throws an exception.

java_io_linenumberinputstream.htm
Advertisements