【发布时间】:2015-06-23 14:19:05
【问题描述】:
我正在使用 BufferedInputStream 并从二进制文件中读取一些数据字段。 我有一个标题,其中包含我必须读取的字段以确定下一条消息需要读取的字节数。所以我需要读取的数据量总是可变的。我尝试一次读取消息的全部字节数,但这有时无法读取消息的全部大小。
所以现在我只是循环并一次读取 1 个字节,直到获得完整的数据消息。这似乎可行,但似乎是读取大数据文件(50 meg 文件)的最有效方式。
从流式二进制文件中读取可变数量数据的最佳方法是什么?
谢谢!!
int numRead= 0;
int total =0;
int readSize =56; // actually read from header
while(numRead>=0 && total < readSize){
// reading one byte at a time
//is there any way to read bigger chunks reliably
// since file is large
numRead= this.in.read(inBytes,total ,1);
if(numRead>0){
total += numRead;
}
}
【问题讨论】:
-
您是从文件中创建
BufferedInputStream吗? -
是的,我正在创建和使用 BufferedInputStream
-
BufferedInputStream buffStream = new BufferedInputStream( new FileInputStream(...
-
我不确定你在问什么。您是在问如何读取整个文件或如何检查此标头然后更改之后读取的数据量?
-
你的 sn-p 中的
this是什么?
标签: java