读取缓冲的二进制文件(与寻求) [英] reading buffered binary file (with seek)
问题描述
$ 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屋!