您的位置:首页 > 大数据

使用split进行大数据分割时内存溢出解决方案

2010-12-07 12:30 435 查看
某个文本文件中存储了60W条记录,以\r\n作为分隔符,现在需要从文本中一次性取出所有值并存放到一个string[]数组中。

StreamReader sr = new StreamReader(strFilePath, System.Text.UnicodeEncoding.GetEncoding("utf-8"));

string strContent = sr.ReadToEnd();

string[] strArr = strContent.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

以上方式非常不建议使用,当数据量很大时很容易抛出内存溢出异常,这是由于string类型自身的安全性导致的,不建议使用string类型的对象临时保存大量的数据。

我们应该采用下面的方式来进行大数据量的处理。

代码

List<string> List = new List<string>();
using (StreamReader _StreamReaderKey = new StreamReader(strTermCacheFilePath + fileInfo.Name))
{
string strLine = "";
while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
{
List.Add(strLine);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐