22

We can read file either by using StreamReader or by using File.ReadAllLines.

For example I want to load each line into a List or string[] for further manipulation on each line.

string[] lines = File.ReadAllLines(@"C:\\file.txt");

foreach(string line in lines)
{
     //DoSomething(line);
}

or

using (StreamReader reader = new StreamReader("file.txt"))
{
    string line;

    while ((line = reader.ReadLine()) != null)
    {
       //DoSomething(line); or //save line into List<string>
    }
}

//if list is created loop through list here 

Application come across different size of text file. Which could grow from few KBs to MBs occasionally.

My question is that which one is preferred way and why one should be preferred over other?

5
  • 3
  • Preferred for what purpose? Speed, memory or?
    – Steve
    Jun 2, 2014 at 7:56
  • @YuvalItzchakov thanks I am looking at the link.
    – Hassan
    Jun 2, 2014 at 7:58
  • @Steve, speed and memory both. If larger size text file comes then application should efficiently read all lines.
    – Hassan
    Jun 2, 2014 at 7:59
  • Then the link above should give you a full answer
    – Steve
    Jun 2, 2014 at 8:01

3 Answers 3

39

If you want to process each line of a text file without loading the entire file into memory, the best approach is like this:

foreach (var line in File.ReadLines("Filename"))
{
    // ...process line.
}

This avoids loading the entire file, and uses an existing .Net function to do so.

However, if for some reason you need to store all the strings in an array, you're best off just using File.ReadAllLines() - but if you are only using foreach to access the data in the array, then use File.ReadLines().

4
  • 2
    @downvoter: Care to explain? Seems odd to downvote the correct answer... ;) Jun 2, 2014 at 9:10
  • Probably downvoted because File.ReadLines() slurp the entire file into memory (a string[]). Jul 27, 2015 at 20:43
  • 8
    @NicholasCarey No, it's the version that DOESN'T do that: public static IEnumerable<string> ReadLines()... hence my comment "without loading the entire file into memory" Jul 28, 2015 at 7:50
  • 1
    @MatthewWatson I agree with you, though it's an old topic - upvoted for anybody going to read this and is beeing confused.
    – TripleEEE
    Nov 30, 2016 at 14:31
25

Microsoft uses a StreamReader in File.ReadAllLines:

    private static String[] InternalReadAllLines(String path, Encoding encoding)
    {
        Contract.Requires(path != null);
        Contract.Requires(encoding != null);
        Contract.Requires(path.Length != 0);

        String line;
        List<String> lines = new List<String>();

        using (StreamReader sr = new StreamReader(path, encoding))
            while ((line = sr.ReadLine()) != null)
                lines.Add(line);

        return lines.ToArray();
    }
5

The StreamReader read the file line by line, it will consume less memory. Whereas, File.ReadAllLines read all lines at once and store it into string[], it will consume more memory. And if that string[] is larger than int.maxvalue then that will produce memory overflow(limit of 32bit OS).

So, for bigger files StreamReader will be more efficient.

1
  • Given Sam's answer about ReadAllLines using StreamReader internally, is this answer invalid? May 3 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.