计算非常大的文件中的行数会给出系统OutofMemory异常

时间:2017-07-19 15:26:38

标签: c#

static void Main(string[] args) 
{

    string TheDataFile = "";
    string ErrorMsg = "";
    string lngTransDate = "";
    ProcessDataFile  ProcessTheDataFile = new ProcessDataFile();

    string TheFile = "S:\\MIS\\Provider NPI file\\Processed\\npidata_20050523-20161009.csv";
    string[] lines = File.ReadAllLines(TheFile, Encoding.UTF8);//Read all lines to an array 
    Console.WriteLine(lines.Length.ToString());
    Console.ReadLine();
}

这会引发错误,因为文件非常大(有600万行)。有没有办法处理大文件并计算行数?

3 个答案:

答案 0 :(得分:4)

使用StreamReader

string TheFile = "S:\\MIS\\Provider NPI file\\Processed\\npidata_20050523-20161009.csv";
int count = 0;
using (System.IO.StreamReader sr = new System.IO.StreamReader(TheFile))
{
    while (sr.ReadLine() != null)
        count++;
}

答案 1 :(得分:2)

您需要对文件进行延迟评估,以使其不会完全加载到内存中。

帮助方法

public static class ToolsEx
{
    public static IEnumerable<string> ReadAsLines(this string filename)
    {
        using (var streamReader = new StreamReader(filename))
            while (!streamReader.EndOfStream)
                yield return streamReader.ReadLine();
    }
}

用法

var lineCount = "yourfile.txt".ReadAsLines().Count();

答案 2 :(得分:2)

According to this already accepted answer,应该这样做。

using System;
using System.IO;

namespace CountLinesInFiles_45194927
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            foreach (var line in File.ReadLines("c:\\Path\\To\\File.whatever"))
            {
                counter++;
            }
            Console.WriteLine(counter);
            Console.ReadLine();
        }
    }
}