1

I tried to split the file about 1GB I do not know of any way to do this I use? StreamReader.ReadLine or File.ReadLines?

Note that I do not get all the data files in memory because it requires more memory.

object json
  • 615
  • 1
  • 8
  • 20
  • 1
    `StreamReader` is more flexible since it has other `Read...` methods. `ReadLines` only splits by lines. So I'd use `ReadLines` where possible and `StreamReader` if necessary, – CodesInChaos Nov 29 '13 at 14:32

2 Answers2

6

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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
-1

ReadAllLines reads ALL the lines in the file in one go. StreamReaders ReadLine reads it line by line but you have to iterate through the file yourself until there is no more lines to read. When you read something.. .of course its going to be in memory regardless.

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72