InputStream, how to jump to the start

R

r.emmerink

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
 
M

Mike Schilling

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)

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?

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?
 
P

Patricia Shanahan

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

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
 
M

Missaka Wijekoon

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

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

-Misk
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top