Java FileReader mark() Method
Example
Try to mark a position in a FileReader, and catch the error:
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("Hello World");
myWriter.close();
try (FileReader myReader = new FileReader("filename.txt")) {
myReader.mark(100);
} catch (IOException e) {
System.out.println("FileReader does not support mark().");
}
}
}
Definition and Usage
The mark() method is meant to mark (remember) the current position, so that reset() can go back to it later.
But FileReader does not support marks. Its mark() method always throws an IOException. To use mark() and reset(), put the FileReader inside a BufferedReader.
Tip: See the BufferedReader mark() method for an example that works.
Syntax
public void mark(int readAheadLimit)
Parameter Values
| Parameter | Description |
|---|---|
| readAheadLimit | Required. The number of characters that can be read after the mark, and still go back. FileReader does not use it. |
Technical Details
| Throws: | IOException - always, because FileReader does not support marks. |
|---|
Related Pages
❮ FileReader Methods