Wednesday, July 3, 2024
HomeLanguagesJavaBufferedReader mark() method in Java with Examples

BufferedReader mark() method in Java with Examples

The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so that same byte can be read again.

Syntax:

public void mark(int readAheadLimit) 
                   throws IOException

Overrides: It overrides the mark() method of Reader class.

Parameters: This method accepts readAheadLimit of Integer type which represents the maximum limit of bytes that can be read before the mark position becomes invalid.

Return value: This method does not return any value.

Exceptions: This method can throw two types of exceptions.

  • IllegalArgumentException – This exception is thrown if the passed parameter readAheadLimit is less than zero.
  • IOException – This exception is thrown if an I/O error occurs.

Below programs illustrate mark() method in BufferedReader class in IO package.
Program 1: Assume the existence of the file “c:/demo.txt”.




// Java program to illustrate
// BufferedReader mark() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // for containing text "GEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        // Read and print characters
        // one by one
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Mark is set on the stream
        buffReader.mark(0);
  
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Reset() is invoked
        buffReader.reset();
  
        // Read and print characters
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
    }
}


Output:

Char : G
Char : E
Char : E
Char : K
Char : K
Char : S

Program 2: Assume the existence of the file “c:/demo.txt”.




// Java program to illustrate
// BufferedReader mark() method
  
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // Read the stream 'demo.txt'
        // containing text "GEEKSFORGEEKS"
        FileReader fileReader
            = new FileReader(
                "c:/demo.txt");
  
        // Convert fileReader to
        // bufferedReader
        BufferedReader buffReader
            = new BufferedReader(
                fileReader);
  
        // Read and print characters
        // one by one
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Mark is set on the stream
        buffReader.mark(0);
  
        System.out.println(
            "Char : "
            + (char)buffReader.read());
  
        // Reset() is invoked
        buffReader.reset();
  
        // read and print characters
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
        System.out.println(
            "Char : "
            + (char)buffReader.read());
    }
}


Output:

Char : G
Char : E
Char : E
Char : K
Char : S
Char : S
Char : F
Char : O
Char : R

References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#mark(int)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments