How to move inputStream back to the first line?

W

www

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.
 
M

Manish Pandit

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.

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
 
P

Print Dude

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.
 
M

Mike Schilling

Print Dude said:
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.

This is what precisely mark() and reset() are for (when they are supported,
that is.)
 
R

Randolf Richardson

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top