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 / April 2006

Tip: Looking for answers? Try searching our database.

InputStream, how to jump to the start

Thread view: 
r.emmerink@gmail.com - 10 Apr 2006 00:03 GMT
Hi,

I am quite new to InputStream. I made a function that reads data from a
file. The function gets an InputStream passed by

public int readStudents(BufferedReader ins) {

and uses (several times)

String regel = ins.readLine();

to read lines. Then I decide how many records I can find in the file
from the contents read. Then I want to read the data of the records,
but I am at the end of the file since I scanned for the records before.
How can I jump to the beginning so I can scan the file again?

Thanks in advance,

Roderik
Mike Schilling - 10 Apr 2006 03:25 GMT
> Hi,
>
> I am quite new to InputStream. I made a function that reads data from a
> file. The function gets an InputStream passed by
>
> public int readStudents(BufferedReader ins) {

a Reader and an InputStream are quite different things.  Readers return
characters and streams return bytes.

> and uses (several times)
>
[quoted text clipped - 4 lines]
> but I am at the end of the file since I scanned for the records before.
> How can I jump to the beginning so I can scan the file again?

Look at the mark() method on BufferedReader.  Note that you'll effectively
have to buffer the entire file in memory to make this work.

Do you really need to count records beore processing the file?
Patricia Shanahan - 10 Apr 2006 05:16 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> Roderik

If you only get an input stream, going back to the start is
theoretically possible, using mark and reset, but that is not how
streams are designed to be used.

Why do you need to go through the stream twice? What is the processing
that requires you to know how many lines you will have?

Patricia
Missaka Wijekoon - 10 Apr 2006 05:29 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> Roderik

Perhaps the RandomAccessFile class might suit you better.  It allows you
to seek within a file.

-Misk


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



©2008 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.