如何使用FileStream读取超过2GB的大文件
如果我有一部电影 (MKV) 且其大小为 7 GB,我如何在 FileStream 中读取它.. 我们知道 int 的最大大小约为 2,147 MB .. 如何从索引 3G.B 开始读取 .. 因为 FileStream 中的 .Read() 方法将偏移量作为整数,而 3 GB 超出了 int 范围.. ???
private void readingLargeFile(string path)
{
int start = 3*(1024*1024*1024);
FileStream fs = new FileStream(path,FileMode.Open);
fs.Read(data, start, (1024*8) );
}
If i have a movie (MKV) and its size like 7 G.B how can i read it in FileStream ..
we know that the maximum size of int is about 2,147 MB .. how to start read from index 3G.B .. since the .Read() method in FileStream takes the offset as an integer which 3 GB is out of int range .. ???
private void readingLargeFile(string path)
{
int start = 3*(1024*1024*1024);
FileStream fs = new FileStream(path,FileMode.Open);
fs.Read(data, start, (1024*8) );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那次阅读并没有达到你想象的那样。
Read
中的偏移量是距开始写入数据的缓冲区开头的偏移量,而不是文件中的偏移量从哪里开始阅读。如果您已经部分填充了缓冲区并且想要添加更多内容,则它通常只是非零:
在该示例中,包含
aaaaaaaabbbbbbbbcccccccc
的文件最终将在缓冲区中如下:您需要首先寻找,并且使用
long
作为偏移值,因此它应该能够很容易地处理8G文件。像这样的事情将是一个很好的起点:Seek
更改为文件的当前位置(它将读取和/或写入的位置,具体取决于您调用的打开模式和函数)。因此,
fs.Seek (start, SeekOrigin.Begin)
会将文件指针设置为从文件开头开始的start
字符。您还可以指定除SeekOrigin.Begin
之外的其他移动方法,从当前位置(例如向前 27 个字节)查找,或从文件末尾查找。MSDN 上提供了关于
Seek
和阅读
。That read doesn't do what you think it does.
The offset in
Read
is the offset from the start of the buffer at which to start writing the data, it is not the offset in the file at which to start reading.It's only usually non-zero if you've already partially populated the buffer and you want to tack on a bit more:
With that example, the file containing
aaaaaaaabbbbbbbbcccccccc
would end up in the buffer as:You need to seek first, and that uses
long
as an offset value so it should be able to handle 8G files quite easily. Something like this would be a good starting point:The
Seek
changes to current position of the file (where it will read from and/or write to, depending on the open mode and functions you call).So
fs.Seek (start, SeekOrigin.Begin)
will set the file pointer tostart
characters from the beginning of the file. You can also specify other methods of movement thanSeekOrigin.Begin
, seeking from the current position like 27 bytes forward, or seeking from the end of the file.Full detail are available on MSDN for both
Seek
andRead
.