BufferedReader.reset() - Can it take you to the start of the stream?
Joined: Oct 15, 2004
If the BufferedReader.reset() method is called and no marks have been set, will the reader move to the start of the stream? (This isn't confirmed by the Javadoc. I'm guessing this isn't the case.)
If this isn't the case, what is the best way to reset a reader to the start of a stream, if this behavior is supported by the stream? Do I need to create a new Reader object to do this?
I'm trying to write a method that accesses the lines in a text file when passed the line number. I need to reset my Reader object to the start of the file if the line number passed to the method is before the current line number being read by the reader.
Note: I don't want to use a RandomAccessFile at this point. I hope to do this in a future implementation.
public void reset() throws IOException Resets the stream to the most recent mark. Overrides: reset in class Reader Throws: IOException - If the stream has never been marked, or if the mark has been invalidated
So if you don't set a mark, it throws an exception. Given your problem, my initial thoughts would be to either close the reader and start from line 0 if I had to go back or use RandomAccessFile and build an in-memory index of the position of each line so I could just jump to the correct position in the file given a particular line number.
I did read the API doc, but I thought I might not be understanding the behavior correctly. You response makes sense. I think I am going to index with a RandomAccessFile after all.
Joined: Jul 11, 2001
Well, it seems to me that you could also simply put a mark at the start of the stream, if the stream supports it.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus