Java BufferedReader reset() Method
Example
Read a character, go back to the mark, and read the same character 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("Hello World");
myWriter.close();
BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
br.mark(100);
System.out.println((char) br.read());
br.reset();
System.out.println((char) br.read());
br.close();
}
}
Definition and Usage
The reset() method moves the reader back to the position that was marked with mark(). The characters after the mark can then be read again.
You must call mark() first. If not, reset() throws an IOException.
Tip: Use the mark() method to mark a position.
Syntax
public void reset()
Parameter Values
None.
Technical Details
| Throws: | IOException - if the reader was never marked, or if the mark was lost because too many characters were read after it. |
|---|
Related Pages
❮ BufferedReader Methods