读取缓冲的二进制文件(带查找)

发布于 2024-10-06 02:20:14 字数 626 浏览 0 评论 0原文

假设我需要读取巨大的整数二进制文件,一个方便的方法是:

FileInputStream fi = new FileInputStream(file);
BufferedInputStream bi = new BufferedInputStream( fi); 
DataInputStream di =new DataInputStream(bi);

但现在假设我必须读取从第 n 个整数开始的巨大块。 到目前为止,我已经自己实现了一种缓冲区:

RandomAccessFile fp=new RandomAccessFile(file);
fp.seek(position);
byte[] buff= new  byte[len];
fp.read(buff, 0, len);
ByteArrayInputStream bIn = new ByteArrayInputStream(buff);
DataInputStream dIn= new DataInputStream(bIn);

现在我可以解析 buff 中的数据,处理它,然后读取下一个块。

我想知道是否有一些我可以使用的标准缓冲区对象。我想简化我的代码,而不是自己处理缓冲。

欢迎任何提示。 雅各布

Say I need to read huge binary file of integers, a handy way is:

FileInputStream fi = new FileInputStream(file);
BufferedInputStream bi = new BufferedInputStream( fi); 
DataInputStream di =new DataInputStream(bi);

But now say I have to read a huge block starting from the n-th integer.
So far I have implemented a sort of buffer by myself as:

RandomAccessFile fp=new RandomAccessFile(file);
fp.seek(position);
byte[] buff= new  byte[len];
fp.read(buff, 0, len);
ByteArrayInputStream bIn = new ByteArrayInputStream(buff);
DataInputStream dIn= new DataInputStream(bIn);

now I can parse the data in buff, process it and then read the next block.

I was wondering if there was some standard buffer object I could have used. I would like to simplify my code and not to take care of the buffering by myself.

Any hint is welcome.
Jacopo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-10-13 02:20:14

看看蔚来。例如,java.nio.MappedByteBuffer。

Have a look at NIO. For example, java.nio.MappedByteBuffer.

一江春梦 2024-10-13 02:20:14

只需从 fi.skip(position) 开始,然后用 bi 和 di 包装它。当position足够大时,底层流实际上会调用seek。

Just start with fi.skip(position) before wrapping it with bi and di. The underlying stream actually makes a call to seek when position is sufficiently large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文