FileStream.Read()定义为:
public override int Read(
byte[] array,
int offset,
int count
)
如何从大于int.MaxValue的偏移量中读取一些字节?
假设我有一个非常大的文件,我想从位置3147483648开始读取100MB.
我怎样才能做到这一点?
最佳答案
这里的偏移量是数组中开始写入的偏移量.在你的情况下,只需设置:
stream.Position = 3147483648;
然后使用Read().当您知道需要读取[n]个字节时,最常使用偏移量:
int toRead = 20, bytesRead;
while(toRead > 0 && (bytesRead = stream.Read(buffer, offset, toRead)) > 0)
{
toRead -= bytesRead;
offset += bytesRead;
}
if(toRead > 0) throw new EndOfStreamException();
这将准确读取20个字节到缓冲区(或抛出异常).请注意,Read()不能保证一次读取所有需要的数据,因此通常需要增加偏移量的循环.
相关文章
转载注明原文:c# – FileStream,从大文件中读取数据块. Filesize大于int.如何设置偏移量? - 代码日志