Java.io.FilterInputStream.mark() Method


Advertisements


Description

The java.io.FilterInputStream.mark(int readlimit) method marks the current position in this input stream.

Declaration

Following is the declaration for java.io.FilterInputStream.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.FilterInputStream.mark(int readlimit) method.

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilterInputStreamDemo {

   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      FilterInputStream fis = null; 
      
      try {
      
         // create input streams
         is = new FileInputStream("C://test.txt");
         fis = new BufferedInputStream(is);
         
         // reads and prints BufferedReader
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // mark invoked at this position
         fis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // reset() repositioned the stream to the mark
         fis.reset();
         System.out.println("reset() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());

      } catch(IOException e) {
         
         e.printStackTrace();
      } finally {
         
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.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 −

ABCDEF

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

A
B
mark() invoked
C
D
reset() invoked
C
D

java_io_filterinputstream.htm

Advertisements