Java BufferedReader mark() Method
Example
Mark the start of a file, read a line, then go back and read the same line again:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Volvo\nBMW\nFord");
myWriter.close();
BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
br.mark(100);
System.out.println(br.readLine());
br.reset();
System.out.println(br.readLine());
br.close();
}
}
Definition and Usage
The mark() method marks (remembers) the current position in the reader. Later, you can call reset() to go back to this position and read the same characters again.
The parameter is the read-ahead limit: the number of characters you can read after the mark, and still go back to it.
Tip: Use the reset() method to go back to the marked position.
Syntax
public void mark(int readAheadLimit)
Parameter Values
| Parameter | Description |
|---|---|
| readAheadLimit | Required. The number of characters you can read after the mark, and still go back with reset(). If you read more characters than this, the mark can be lost. |
Technical Details
| Throws: | IllegalArgumentException - if readAheadLimit is negative. |
|---|
Related Pages
❮ BufferedReader Methods