Read Big TXT File, Out of Memory Exception

前端 未结 4 1694
孤独总比滥情好
孤独总比滥情好 2020-11-29 12:19

I want to read big TXT file size is 500 MB, First I use

var file = new StreamReader(_filePath).ReadToEnd();  
var lines = file.Split(new[] { \'\\n\' });
         


        
4条回答
  •  醉酒成梦
    2020-11-29 12:49

    Just use File.ReadLines which returns an IEnumerable and doesn't load all the lines at once to the memory.

    foreach (var line in File.ReadLines(_filePath))
    {
        //Don't put "line" into a list or collection.
        //Just make your processing on it.
    }
    

提交回复
热议问题