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