Java FilterInputStream mark() Method
In this tutorial, we will learn about the mark() method of FilterInputStream class in Java. This method is used to set the current position in the particular Filter Input Stream. A subsequent invoking of the reset() method will reposition the stream to the most recently marked point by this method. It is a non-static method, available in the java.io package.
Syntax
This is the syntax declaration of this method. It accepts only one parameter as the limit of the maximum number of bytes that can be read from the current input stream before the mark position becomes invalid. It does not return any value.
public void mark(int readlimit)
Example 1: Set the Current Position in Stream
In this example, we are marking the initial position of the stream by calling the method mark() and we read the two characters just after reading the two characters we call reset() method to set the pointer of the current stream to the recently marked position and if we try to read then it will give output data from the lastly marked position.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String args[])
{
try
{
File file = new File("E:\\studytonight\\output.txt");
FileInputStream fis = new FileInputStream(file);
FilterInputStream filterIs = new BufferedInputStream(fis);
filterIs.mark(0);
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
System.out.println("Last mark call at 0th index:- ");
filterIs.reset();
System.out.println("reset() called ");
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
fis.close();
filterIs.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
H
e
Last mark call at 0th index:-
reset() called
H
e
output.txt
Hello Studytonight
Example 2: Set the Current Position in Stream
In this example, we are calling mark() and reset() methods consecutively so it will give the same output as given by the read() method normally because the recently marked position and current reading position in the stream are same.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String args[])
{
try
{
File file = new File("E:\\studytonight\\output.txt");
FileInputStream fis = new FileInputStream(file);
FilterInputStream filterIs = new BufferedInputStream(fis);
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
filterIs.mark(0);
filterIs.reset();
System.out.println("reset() called ");
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
fis.close();
filterIs.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
H
e
reset() called
l
l
o
output.txt
Hello Studytonight
Example 3: Set the Current Position in Stream
Here, we are looking at which mark() method will be considered if the multiple methods found, after looking at the output we can draw a conclusion that it will only consider the last recently marked position.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String args[])
{
try
{
File file = new File("E:\\studytonight\\output.txt");
FileInputStream fis = new FileInputStream(file);
FilterInputStream filterIs = new BufferedInputStream(fis);
System.out.println((char) filterIs.read());
filterIs.mark(0);
System.out.println((char) filterIs.read());
filterIs.mark(0);
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
filterIs.reset();
System.out.println("reset() called ");
System.out.println((char) filterIs.read());
System.out.println((char) filterIs.read());
fis.close();
filterIs.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
H
e
l
l
o
reset() called
l
l
Conclusion:
In this tutorial, we learned about the mark() method of FilterInputStream class in Java, which sets the mark to the current position in the given filter input stream and a subsequent call of reset() method will reposition the stream to the most recent point marked by the mark() method. It is accessible with the class object only and if we try to access the method with the class name, we will get an error.