read huge file line by line in c# [duplicate]

流过昼夜 提交于 2019-12-12 03:23:19

问题


I have huge files in c# (more than 300 MB). I need effiecent way to read the file line by line because once I try to read it it takes more than 30 minutes while the target time is around 3 minutes. I tried File.ReadAllBytes which reads the files successfully and very fast and load it to the string. But after that it takes very long time to process the string line by line. Is there better way or faster way to do so.

Thank in advance.


回答1:


You can use File.ReadLines, it will enumerate through lines of file:

var lines = File.ReadLines(path);

foreach(var line in lines)
{
    // do your logic here
}

It will not load file at the first line. It will load it while looping through lines, so it's better way to read bigger files, than loading it at once.

MSDN says in description of File.ReadLines

Remarks The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.



来源:https://stackoverflow.com/questions/33515571/read-huge-file-line-by-line-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!