C#进行大数据文本按行读取分割时内存溢出解决方案

作者: drmeng 分类: 编程学习,随笔 发布时间: 2020-02-24 23:41

如果一个文本文件的数据条数超过100万行,直接读取到string里面会出错。

public static string[] GetFileContentArray(string FilePath)
{
//支持2G至4G文件读取
List List = new List();
using (StreamReader _StreamReaderKey = new StreamReader(FilePath))
{
string strLine = “”;
while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
{
if (strLine.Trim()==””)
{
continue;
}
List.Add(strLine.Trim());
}
}
return List.ToArray();
}