Hi,
I have an inputStream to read a text file:
BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
String line = inputStream.readLine();
After reading several lines, I hope to bring inputStream back to the
first line. How can I do it?
Right now, I do:
inputStream.close();
BufferedReader inputStream = new BufferedReader(new
FileReader(fileName)); //start over again. I feel it is a dumb way.
Thank you.
Manish Pandit - 26 Jan 2007 19:13 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> Thank you.
One way could be to use mark( ) to mark the beginning of the file, and
then do a reset( ) which will take the pointer to the mark (beginning).
I do not think you can call reset after closing the stream though.
http://java.sun.com/j2se/1.4.2/docs/api/java/io/Reader.html#reset()
-cheers,
Manish
Print Dude - 27 Jan 2007 02:50 GMT
> BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
> String line = inputStream.readLine();
>
> After reading several lines, I hope to bring inputStream back to the
> first line. How can I do it?
I had a similar issue a while ago. What I ended up doing was reading
the entire file into an ArrayList so that I could traverse it
sequetially.
Mike Schilling - 27 Jan 2007 06:35 GMT
>> BufferedReader inputStream = new BufferedReader(new
>> FileReader(fileName));
[quoted text clipped - 6 lines]
> the entire file into an ArrayList so that I could traverse it
> sequetially.
This is what precisely mark() and reset() are for (when they are supported,
that is.)
Randolf Richardson - 28 Jan 2007 07:09 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> Thank you.
There is a much easier way to read a text file which involves only one
class for all your I/O instead of the typical Java examples used to read a
file that use two or three (and sometimes more):
http://www.internationalnetwork.com/java/examples/DisplayFile.java
Then, use the java.io.RandomAccessFile.seek() method to change the
pointer back to 0.

Signature
Randolf Richardson - kingpin+nntp@lumbercartel.ca
The Lumber Cartel, local 42 (Canadian branch)
http://www.lumbercartel.ca/