Differencebetween File.Readlines() and File.Readalllines()

What is the difference between File.ReadLines() and File.ReadAllLines()?


is there any performance difference related to these methods?

YES there is a difference

File.ReadAllLines() method reads the whole file at a time and returns the string[] array, so it takes time while working with large size of files and not recommended as user has to wait untill the whole array is returned.

File.ReadLines() returns an IEnumerable<string> and it does not read the whole file at one go, so it is really a better option when working with large size files.

From MSDN:

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.

Example 1: File.ReadAllLines()

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

Example 2: File.ReadLines()

foreach (var line in File.ReadLines("C:\\mytxt.txt"))
{

//Do something

}

What is the difference between File.ReadAllLines() and File.ReadAllText()?

ReadAllLines returns an array of strings. Each string contains a single line of the file.

ReadAllText returns a single string containing all the lines of the file.

The difference between StreamReader.ReadLine and File.ReadLines?

File.ReadLines internally creates ReadLinesIterator which uses StreamReader.ReadLine() to read file line by line when you are enumerating lines:

internal class ReadLinesIterator : Iterator<string>
{
private StreamReader _reader;

public override bool MoveNext()
{
if (this._reader != null)
{
base.current = this._reader.ReadLine();
if (base.current != null)
return true;

base.Dispose();
}
return false;
}
}

So, difference is following - StreamReader.ReadLine() reads single line from stream. File.ReadLines iterates over all lines (until you stop) and uses StreamReader.ReadLine() for reading each single line from stream.

Need help in understanding the explanation by Microsoft for File.ReadLines and File.ReadAllLines

The difference between ReadLines and ReadAllLines is easily illustrated by code.

If you write this:

foreach (var line in File.ReadLines(filename))
{
Console.WriteLine(line);
}

What happens is similar to this:

using (var reader = new StreamReader(filename))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
Console.WriteLine(line);
}
}

The actual code generated is a little more complex (ReadLines returns an enumerator whose MoveNext method reads and returns each line), but from the outside the behavior is similar.

The key to that behavior is deferred execution, which you should understand well in order to make good use of LINQ. So the answer to your first question is "No." All the call to ReadLines does is open the file and return an enumerator. It doesn't read the first line until you ask for it.

Note here that the code can output the first line before the second line is even read. In addition, you're only using memory for one line at a time.

ReadAllLines has much different behavior. When you write:

foreach (var line in File.ReadAllLines(filename))
{
Console.WriteLine(line);
}

What actually happens is more like this:

List<string> lines = new List<string>();
using (var reader = new StreamReader(filename))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
lines.Add(line);
}
}
foreach (var line in lines)
{
Console.WriteLine(line);
}

Here, the program has to load the entire file into memory before it can output the first line.

Which one you use depends on what you want to do. If you just need to access the file line-by-line, then ReadLines is usually the better choice--especially for large files. But if you want to access lines randomly or if you'll be reading the file multiple times, then ReadAllLines might be better. However, remember that ReadAllLines requires that you have enough memory to hold the entire file.

In your third question you showed this code, which produced an exception on the last line:

IEnumerable<String> filedata = File.ReadLines(fileWithPath);
string lastLine = filedata.Last();
int totalLines = filedata.Count();

What happened here is that the first line returned an enumerator. The second line of code enumerated the entire sequence (i.e. read to the end of the file) so that it could find the last line. The enumerator saw that it was at end of file and closed the associated reader. The last line of code again tries to enumerate the file, but the file was already closed. There's no "reset to the start of the file" functionality in the enumerator returned by ReadLines.

File.ReadLines and convert type error

Try this:

System.Collections.Generic.IEnumerable<String> lines = File.ReadLines(Path.Text, Encoding.Unicode);

Note that File.ReadLines() returns an object of type System.Collections.Generic.IEnumerable<String>

Any difference between File.ReadAllText() and using a StreamReader to read file contents?

There are no differences if you are using the ReadToEnd() method. The difference is if you are using the ReadLine() method for large files as you are not loading the whole file into memory but rather allows you to process it in chunks.

So use File.ReadAllText() instead of ReadToEnd() as it makes your code shorter and more readable. It also takes care of properly disposing resources as you might forget doing with a StreamReader (as you did in your snippet).

OpenText vs ReadLines

Objectively:

  • The first one is picking the line one by one and you do the process
    as you stream the data.

  • The second one generates IEnumerable<string> at once and then you can
    start processing the lines (Source MSDN - my apologize to mix it
    with ReadAllLines at first).

Usefulness?:

  • The first one is more "fluid" in that sense. Because you can choose
    to take/process and break/continue at will. Everytime you need a line, you take one, you process it and then you choose to break or continue. You can even choose to not take any further line (suppose you have yield in the while loop) till you want to come back at will
  • The second one would get IEnumerable<string> (that is, to specify the info of the expected data before process) thus giving slightly overhead at first. It is to be noted, however, that this overhead is relatively small as IEnumerable defer the actual execution, unlike List. Some good info to read.

And from MSDN The following are the use cases for ReadLines:

Perform LINQ to Objects queries on a file to obtain a filtered set of
its lines.

Write the returned collection of lines to a file with the
File.WriteAllLines(String, IEnumerable<String>) method, or append them
to an existing file with the

File.AppendAllLines(String, IEnumerable<String>) method. Create an
immediately populated instance of a collection that takes an
IEnumerable<T> collection of strings for its constructor, such as a
IList<T> or a Queue<T>.

File.ReadAllLines or Stream Reader

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



Related Topics



Leave a reply



Submit