开发者

choice of implementation to read lines of file - c#

开发者 https://www.devze.com 2023-03-27 10:08 出处:网络
The files at max could be 1GB came across these 2 reads IEnumerable and How to: Read Text from a File Which one is better开发者_StackOverflow社区 in terms of efficiency - memory/cpu

The files at max could be 1GB

came across these 2 reads

IEnumerable

and

How to: Read Text from a File

Which one is better开发者_StackOverflow社区 in terms of efficiency - memory/cpu

Also, where can i read up on what stream reader implements under the hood??


Neither - if you're using .NET 4, use File.ReadLines.

If you're using .NET 3.5 or earlier, you can write this yourself more easily than the VB page showed, as VB doesn't have iterator blocks yet (it's getting them in the next version, and in a more flexible way than in C#):

// Optional: create an overload of this taking an encoding
public IEnumerable<string> ReadLines(string file)
{
    using (TextReader reader = File.OpenText(file))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line;
        }
    }
}

This is effectively what File.ReadLines does for you anyway.

Then you can use:

foreach (string line in ReadLines("file.txt"))
{
    // Use a line at a time
}

No more than a line or so (the size of the buffer) will be in memory at a time, and it'll be reasonably CPU-efficient too.

All of these will use StreamReader under the hood, which is right and proper as it's the canonical way of converting a stream of binary data to a TextReader. Basically it reads binary data from a Stream and uses an Encoding to convert that data into text as and when it's requested. What more do you need to know?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号