读取大文件 [英] Read large files

查看:74
本文介绍了读取大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过两种方式读取2+ gb文件,第一种:

I tried to read a 2+ gb file in two ways, the first:

var file = File.ReadAllBytes(filepath);

返回一个异常,文件大小超过2GB.

returns an exception, file over 2gb.

第二种方式:

var file = ReadAllBytes(filepath);

public byte[] ReadAllBytes(string fileName)
{
    byte[] buffer = null;

    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        buffer = new byte[fs.Length];
        fs.Read(buffer, 0, (int)fs.Length);
    }

    return buffer;
}

异常:数组尺寸超出了支持的范围." 我的目标是将文件发送到http请求的正文中(使用WebClient类).

Exception: "Array dimensions exceeded supported range." My goal is to send the file in the body of http request (using WebClient class).

有任何有关读取大文件的示例吗?

Any example of how to read large files?

谢谢

推荐答案

您可以尝试以下操作:

public void ProcessLargeFile(string fileName)
{
    int bufferSize = 100 * 1024 * 1024; // 100MB
    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;

    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        while ((bytesRead = fs.Read(buffer, 0, bufferSize)) > 0)
        {
            if (bytesRead < bufferSize)
            {
                // please note array contains only 'bytesRead' bytes from 'bufferSize'
            }

            // here 'buffer' you get current portion on file 
            // process this
        }
    }
}

这将允许您按100MB的部分处理文件,您可以将此值更改为必需的值.

That will allow you to process file by 100MB portions, you can change this value to required one.

这篇关于读取大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆