读取缓冲的二进制文件(带查找)
假设我需要读取巨大的整数二进制文件,一个方便的方法是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看蔚来。例如,java.nio.MappedByteBuffer。
Have a look at NIO. For example, java.nio.MappedByteBuffer.
只需从 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.