如何从指定位置读取大型二进制文件?
我不想从文件开头开始读取,因为我已经计算出了需要读取的流的起始位置和长度。
using (FileStream sr = File.OpenRead("someFile.dat"))
{
sr.Seek(100, SeekOrigin.Begin);
int read = sr.ReadByte();
//...
}
根据 @shenhengbin 的回答。
使用 BinaryReader.BaseStream.Seek
。
using (BinaryReader b = new BinaryReader(File.Open("perls.bin", FileMode.Open)))
{
int pos = 50000;
int required = 2000;
// Seek to our required position.
b.BaseStream.Seek(pos, SeekOrigin.Begin);
// Read the next 2000 bytes.
byte[] by = b.ReadBytes(required);
}
如果你了解流,为什么不使用(File)Stream.Seek(...)?
Position
属性或使用Seek
方法。你担心整个文件都会加载到内存中吗? - Marlon