views:

98

answers:

2

As a java guru, what would you suggest to abstract random access so that a code can be agnostic to whether the data its accessing is in memory or in a file on the harddrive?

(The files in question could be several gigabytes in size. Having random access is the most important feature.)

+1  A: 

This is what Java NIO is for.

See Random Access Files and Memory-mapped files.

cletus
This looks like a good solution for my application. Thank you very much Stack Overflow, and cletus and BalusC and bmargulies and danben!
Jason Knight
+1  A: 

I can offer memory mapping of files. Those give you a java.nio.ByteBuffer. And you can also make those as ordinary in-memory objects, so there you go, perfect agnosis, if not perfect ignorance.

And we've had them since 1.4.

The question is in some respects unclear. If you want file-style access, you are rather out of luck, since a RandomAccessFile can't be constructed over any in-memory resource.

bmargulies