Chr*_*ris 5 c# file stream filestream
FileStream.Read()定义为:
public override int Read(
    byte[] array,
    int offset,
    int count
)
如何从大于int.MaxValue的偏移量中读取一些字节?
假设我有一个非常大的文件,我想从位置3147483648开始读取100MB.
我怎样才能做到这一点?
Mar*_*ell 11
这offset是数组中开始写入的偏移量.在你的情况下,只需设置:
stream.Position = 3147483648;
然后使用Read().offset当您知道需要读取[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个字节buffer(或抛出异常).注意,Read()不能保证一次读取所有需要的数据,因此通常需要增加偏移量的循环.
| 归档时间: | 
 | 
| 查看次数: | 8757 次 | 
| 最近记录: |