Java.io.LineNumberInputStream.mark() Method



Description

The java.io.LineNumberInputStream.mark(int readlimit) method marks the current position in this input stream. A subsequent call to the reset method repositions this stream so that the subsequent reads re-read the same byte.

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

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

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("C:/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();      
      }
   }
}

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

ABCDE

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

A
B
mark() invoked
C
D
java_io_linenumberinputstream.htm
Advertisements