读大文件

时间:2018-03-13 12:32:45

标签: c# file io filestream

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

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;
}

返回一个超过2gb的异常文件。

第二种方式:

TryParse

例外:"阵列尺寸超出了支持范围。" 我的目标是在http请求的主体中发送文件(使用WebClient类)。

如何阅读大文件的任何例子?

由于

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

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部分处理文件,您可以将此值更改为所需的值。

答案 1 :(得分:-1)

您正在遇到一个相当古老的限制,即用户模式虚拟地址空间的2 GiB限制。您可以使用正确的编译器/清单开关和/或在x64模式下运行相同的程序来稍微提升它。你可以在这里详细阅读: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366778.aspx?f=255&MSPPError=-2147217396#memory_limits

在某些大小,文件或查询结果集无法完全加载到内存中(由于限制),或者在开始处理之前最好不能完全加载(由于性能)。对于这些情况,调查员是有帮助的。将File.ReadLinesFile.ReadAllLines进行比较。由于使用了枚举器,ReadLines只需要在内存中保留一行至少 - 当前行。可以删除已处理的所有行。仍然在未来的人可能已加载或未加载。在任何时候都不需要将完整文件加载到内存中。

不幸的是,File.ReadAllBytes似乎没有枚举变体。似乎不同的类BinaryReader确实具有ReadBytes的形式的这种能力。

相关问题