当前位置:编程学习 > C#/ASP.NET >>

StreamReader 读取大文件报错OutOfMemoryException

我写了 一段代码读取解析文件内容如下,被读取的文件的大约780M 读到845715行的时候提示错误
此时PC内存使用高达2G,请问如何解决或者可用其他方式读取 大文件?

 ArrayList oldFileList=new ArrayList(2000);
 StreamReader reader = new StreamReader(filepath, Encoding.GetEncoding("gb2312")); 
 string n1 = string.Empty; 
            
            try
            {
                while (reader.Peek() > -1)
                {                   
                      str = reader.ReadLine(); // 这里报错                                                                                        
                       
                    if (str.Length > 5)
                    {
                         n1 = str.Substring(0, 4);
                        if (n1 == "A1")
                        {
                            oldFileList.Add(str);
                        }
                        else if (n1 == "B1")
                        {
                            oldFileList.Add(str);
                        }
                    }
                    str = null;
                    n1 = null; 
                    iCount++;
                  
                    if (iCount % 100 == 0)
                    {
                        GC.Collect(); 
                    }                   
                }                   
               
            }

            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
                reader.Dispose();                
            }
            return oldFileList;


引发类型为“System.OutOfMemoryException”的异常。
StreamReader OutOfMemoryException --------------------编程问答-------------------- 用stream实现分块读取
http://bbs.csdn.net/topics/350230269 --------------------编程问答-------------------- ArrayList oldFileList=new ArrayList(2000);
是不是这里值设得太小有关?
ArrayList oldFileList=new ArrayList();默认的看一下 --------------------编程问答-------------------- 我还是建议用内存映射读取大文件
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,