I want to be able to open a (very large) file and read data from arbitrary points at any time later, without keeping the whole thing in memory.
Is this possible?
I want to open a handle to a file, nothing else will be reading/writing to this file, and be able to grab data from any given offset & length at a point later, without searching the disk for the file again.
I had assumed there would be some sort of
1, open handle to file
2, create reader
3, skip reader to X
4, read for Y
5, reset reader to 0
6, return to step 3
At first it seemed like BufferedReader.mark would be what I’m after, but the docs suggest that if I mark, skip, read, reset the contents from where I marked, to where I skipped, would be kept in memory?
From the docs,
Limit on the number of characters that may be read while still
preserving the mark. An attempt to reset the stream after reading
characters up to this limit or beyond may fail. A limit value larger
than the size of the input buffer will cause a new buffer to be
allocated whose size is no smaller than limit. Therefore large values
should be used with care.
It says “read”, so perhaps if I skip, it doesn’t count towards this limit (that makes sense), but I might be reading quite a bit, so it seems this newly allocated input buffer could get pretty large. I don’t want that to happen…
Is there a better way to be going about this?
You probably are looking for the
RandomAccessFileclass.