FileStream,从大文件中读取数据块。文件大小大于 int。如何设置偏移量?
FileStream.Read() 定义为:
public override int Read(
byte[] array,
int offset,
int count
)
如何从大于 int.MaxValue 的偏移量读取一些字节?
假设我有一个非常大的文件,我想从位置 3147483648 开始读取 100MB。
我该怎么做?
FileStream.Read() is defined as:
public override int Read(
byte[] array,
int offset,
int count
)
How can I read some bytes from an offset bigger than int.MaxValue?
Let's say I have a very big file and I want to read 100MB starting from position 3147483648.
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的
offset
是数组中开始写入的偏移量。在您的情况下,只需设置:然后使用
Read()
。当您知道需要读取 [n] 个字节时,最常使用offset
:这会将 20 个字节读入
buffer
(或抛出异常)。请注意,Read()
不能保证一次性读取所有所需的数据,因此通常需要一个递增偏移量的循环。The
offset
here is the offset in the array at which to start writing. In your case, just set:and then use
Read()
. Theoffset
is most commonly used when you know you need to read [n] bytes:this will read exactly 20 bytes into
buffer
(or throw an exception). Note thatRead()
is not guaranteed to read all the required data in one go, hence a loop incrementing an offset is usually required.根据 http://msdn.microsoft.com/en -us/library/system.io.filestream.read.aspx
offset
参数是 byte[] 数组内的偏移量
:Read()
只是从当前位置读取,该位置恰好是一个long
并且应该在调用Read()
之前设置,请参阅 http://msdn.microsoft.com/en-us/library/system .io.filestream.position.aspxAccording to http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx the
offset
parameter is anoffset inside the byte[] array
:Read()
just reads from the current positon which happens to be along
and should be set before callingRead()
see http://msdn.microsoft.com/en-us/library/system.io.filestream.position.aspx