Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading a file chunk by chunk

806067May 9 2011 — edited May 11 2011
Hi All!
I'm trying to read a file, and separate it into pieces each of which are 4096 bytes. Also, each piece will be numbered from 0 to end of file. So, I did this:
        InputStream in = new FileInputStream(file);
        byte[] bytes = new byte[4096];
        int frameID = 0;

        while (in.read(bytes, 0, bytes.length) != -1) {
                frameID++;
                bytes = new byte[4096];
        }

        in.close();
So, I just iterate through every 4096 bytes of the file , and put the content to the byte[]. However, I didn't come up with a better idea, because when I wanna read this file again for a specific chunk (e.g frameID=15;), I have to start while loop from the beginning until I come up the frameId, then use the byte[]. It's quite silly, I'm aware of it.

Basically, I have to read a file chunk by chunk, and I should be able to jump to a specific chunk directly. I am not allowed to hold chunks in a collection, then use it from there. Because, it's mandatory that I need to access harddisk to read when I need a piece of data. Is there any idea how can I do that? Thanks!!
This post has been answered by sabre150 on May 9 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2011
Added on May 9 2011
11 comments
1,426 views