Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

How to move inputStream back to the first line?

Thread view: 
www - 26 Jan 2007 18:54 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.