I am using class BufferedReader
to read line by line in the buffer. When reading the last line in the buffer, I want to start reading from the beginning of the buffer again.
I have read about the mark()
and reset()
, I am not sure its usage but I don't think they can help me out of this.
Does anyone know how to start reading from the beginning of the buffer after reaching the last line? Like we can use seek(0)
of the RandomAccessFile
?
mark/reset is what you want, however you can't really use it on the BufferedReader, because it can only reset back a certain number of bytes (the buffer size). if your file is bigger than that, it won't work. there's no "simple" way to do this (unfortunately), but it's not too hard to handle, you just need a handle to the original FileInputStream.
FileInputStream fIn = ...;
BufferedReader bRead = new BufferedReader(new InputStreamReader(fIn));
// ... read through bRead ...
// "reset" to beginning of file (discard old buffered reader)
fIn.getChannel().position(0);
bRead = new BufferedReader(new InputStreamReader(fIn));
(note, using default character sets is not recommended, just using a simplified example).