Java 1.6 抽象随机访问
作为 Java 专家,您建议如何抽象随机访问,以便代码可以不知道其访问的数据是在内存中还是在硬盘驱动器上的文件中?
(相关文件的大小可能有几千兆字节。随机访问是最重要的功能。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是 Java NIO 的用途。
请参阅随机访问文件和内存映射文件。
This is what Java NIO is for.
See Random Access Files and Memory-mapped files.
我可以提供文件的内存映射。这些给你一个java.nio.ByteBuffer。你也可以将它们作为普通的内存对象,所以你就可以实现完美的无知,即使不是完全的无知。
我们从 1.4。
这个问题在某些方面尚不清楚。如果您想要文件式访问,那么您就不太走运了,因为无法在任何内存资源上构造 RandomAccessFile。
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.