user1300214
user1300214

Reputation:

Java: Using multiple DataInputStreams on single FileInputStream

Suppose I have the following code in Java:

        FileInputStream fin = new FileInputStream(filename);
        DataInputStream x = new DataInputStream(fin);
        DataInputStream y = new DataInputStream(fin);
        DataInputStream z = new DataInputStream(fin);

I want to use y.skip(100) and z.skip(200) to simultaneously read data from the file at different positions. Will this work? I'm getting EOF errors at the moment...

EDIT

I did try the following code:

        FileInputStream fin1 = new FileInputStream(filename);
        FileInputStream fin2 = new FileInputStream(filename);
        FileInputStream fin3 = new FileInputStream(filename);
        DataInputStream x = new DataInputStream(fin1);
        DataInputStream y = new DataInputStream(fin2);
        DataInputStream z = new DataInputStream(fin3);

This does not produce EOF errors, but still not sure if this might return corrupted data?...

Upvotes: 0

Views: 81

Answers (2)

user207421
user207421

Reputation: 311023

In this specific case it won't produce corrupted data, but if there had been a BufferedInputStream anywhere in association with them it would have.

The real question is why on earth you would want to do this? Why can't you use the same DataInputStream?

Upvotes: 0

user1300214
user1300214

Reputation:

I seem to have found a solution. The original doesn't work because it just increments the file pointer every time, regardless of the DataInputStream used. Instead I needed to create additional FileInputStreams's. Works fine.

Upvotes: 1

Related Questions