This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes BufferedReader.reset() - Can it take you to the start of the stream? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "BufferedReader.reset() - Can it take you to the start of the stream?" Watch "BufferedReader.reset() - Can it take you to the start of the stream?" New topic
Author

BufferedReader.reset() - Can it take you to the start of the stream?

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
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.

Thanks,

Landon
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8263

From the Java API Documentation:

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 43
Joe,

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.

Thanks.

Landon
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: BufferedReader.reset() - Can it take you to the start of the stream?
 
Similar Threads
Vectors passed to a static method?
Scanning a text file
Ranchers,Clarification on UTF-8 in Java
using BufferedReader and looping
SCJD passed