Home »
Java »
Java Reference »
Java BufferedInputStream Class
Java BufferedInputStream mark() Method with Example
BufferedInputStream Class mark() method: Here, we are going to learn about the mark() method of BufferedInputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 01, 2020
BufferedInputStream Class mark() method
- mark() method is available in java.io package.
- mark() method is used to set the limit of bytes to be read before the given set limit or marked position.
- mark() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- mark() method does not throw an exception at the time of setting the limit.
Syntax:
public void mark(int m_limit);
Parameter(s):
- int m_limit – represents the number of bytes to read before the marked limit (m_limit).
Return value:
The return type of the method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void mark(int m_limit) method of
// BufferedInputStream
import java.io.*;
public class MarkBIS {
public static void main(String[] args) throws Exception {
// To open text file by using
// FileInputStream
FileInputStream fis = new FileInputStream("e:/includehelp.txt");
// By using BufferedInputStream() isto change
// the stream fis into buff_str
BufferedInputStream buff_str = new BufferedInputStream(fis);
// By using available() method is to
// return the no. of bytes to be left
// for reading
Integer n_byte = buff_str.available();
System.out.println("Left avail bytes = " + n_byte);
// Read character from the stream
char ch1 = (char) buff_str.read();
char ch2 = (char) buff_str.read();
char ch3 = (char) buff_str.read();
System.out.println("ch1: " + ch1);
System.out.println("ch2 : " + ch2);
// By using mark() method isto
// set the limit the number of byte
// to be read
buff_str.mark(5);
System.out.println("ch3: " + ch3);
// reset stream by using reset()
buff_str.reset();
// Read from the stread
char ch4 = (char) buff_str.read();
char ch5 = (char) buff_str.read();
// Display character
System.out.println("ch4: " + ch4);
System.out.println("ch5: " + ch5);
fis.close();
buff_str.close();
}
}
Output
Left avail bytes = 33
ch1: H
ch2 : e
ch3: l
ch4: l
ch5: o