25

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
    stackoverflow.com/questions/8037070/… Commented Jun 2, 2014 at 7:52
  • Preferred for what purpose? Speed, memory or? Commented Jun 2, 2014 at 7:56
  • @YuvalItzchakov thanks I am looking at the link. Commented Jun 2, 2014 at 7:58
  • @Steve, speed and memory both. If larger size text file comes then application should efficiently read all lines. Commented Jun 2, 2014 at 7:59
  • Then the link above should give you a full answer Commented Jun 2, 2014 at 8:01

3 Answers 3

43

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().

Sign up to request clarification or add additional context in comments.

5 Comments

@downvoter: Care to explain? Seems odd to downvote the correct answer... ;)
Probably downvoted because File.ReadLines() slurp the entire file into memory (a string[]).
@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"
@MatthewWatson I agree with you, though it's an old topic - upvoted for anybody going to read this and is beeing confused.
Here's a good analysis of what File.ReadLines() is doing and why it is memory efficient: medium.com/@nuno.caneco/…
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(); } 

Comments

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.

2 Comments

Given Sam's answer about ReadAllLines using StreamReader internally, is this answer invalid?
This answer is correct. But you can also use File.ReadLines(), which uses a StreamReader under the hood and maintains memory efficiency.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.