读取缓冲的二进制文件(与寻求) [英] reading buffered binary file (with seek)

查看:157
本文介绍了读取缓冲的二进制文件(与寻求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我需要读取整数大二进制文件,一个方便的方法是:
$ b $ pre $ FileInputStream fi = new FileInputStream(file );
BufferedInputStream bi = new BufferedInputStream(fi);
DataInputStream di = new DataInputStream(bi);

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

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

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

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



欢迎任何提示。
Jacopo

解决方案

刚开始使用fi.skip(position)当位置足够大时,底层流实际上会进行调用。

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

解决方案

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.

这篇关于读取缓冲的二进制文件(与寻求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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