缓冲的 RandomAccessFile java [英] Buffered RandomAccessFile java

查看:27
本文介绍了缓冲的 RandomAccessFile java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RandomAccessFile 对于随机访问文件来说非常慢.您经常阅读有关在其上实现缓冲层的信息,但无法在线找到执行此操作的代码.

RandomAccessFile is quite slow for random access to a file. You often read about implementing a buffered layer over it, but code doing this isn't possible to find online.

所以我的问题是:你们知道这个类的任何开源实现是共享一个指针还是共享你自己的实现?

So my question is: would you guys who know any opensource implementation of this class share a pointer or share your own implementation?

如果这个问题能成为关于这个问题的有用链接和代码的集合,那就太好了,我敢肯定,很多人都分享过这些问题,但 SUN 从未正确解决过这些问题.

It would be nice if this question would turn out as a collection of useful links and code about this problem, which I'm sure, is shared by many and never addressed properly by SUN.

请不要提及 MemoryMapping,因为文件可能比 Integer.MAX_VALUE 大得多.

Please, no reference to MemoryMapping, as files can be way bigger than Integer.MAX_VALUE.

推荐答案

您可以使用类似的代码从 RandomAccessFile 创建 BufferedInputStream,

You can make a BufferedInputStream from a RandomAccessFile with code like,

 RandomAccessFile raf = ...
 FileInputStream fis = new FileInputStream(raf.getFD());
 BufferedInputStream bis = new BufferedInputStream(fis);

注意事项

  1. 关闭 FileInputStream 将关闭 RandomAccessFile,反之亦然
  2. RandomAccessFile 和 FileInputStream 指向相同的位置,因此从 FileInputStream 中读取将推进 RandomAccessFile 的文件指针,反之亦然

可能你想使用它的方式是这样的,

Probably the way you want to use this would be something like,

RandomAccessFile raf = ...
FileInputStream fis = new FileInputStream(raf.getFD());
BufferedInputStream bis = new BufferedInputStream(fis);

//do some reads with buffer
bis.read(...);
bis.read(...);

//seek to a a different section of the file, so discard the previous buffer
raf.seek(...);
bis = new BufferedInputStream(fis);
bis.read(...);
bis.read(...);

这篇关于缓冲的 RandomAccessFile java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆