Reading large text files with streams in C#

后端 未结 11 1833
野的像风
野的像风 2020-11-22 08:28

I\'ve got the lovely task of working out how to handle large files being loaded into our application\'s script editor (it\'s like VBA for our internal product for quick macr

11条回答
  •  无人共我
    2020-11-22 09:03

    If you read the performance and benchmark stats on this website, you'll see that the fastest way to read (because reading, writing, and processing are all different) a text file is the following snippet of code:

    using (StreamReader sr = File.OpenText(fileName))
    {
        string s = String.Empty;
        while ((s = sr.ReadLine()) != null)
        {
            //do your stuff here
        }
    }
    

    All up about 9 different methods were bench marked, but that one seem to come out ahead the majority of the time, even out performing the buffered reader as other readers have mentioned.

提交回复
热议问题