StreamReader.ReadLine和File.ReadLines之间的区别?

时间:2013-11-29 14:06:08

标签: c# split

我试图将文件拆分大约1GB我不知道有什么方法可以这样做我用的? StreamReader.ReadLine或File.ReadLines?

请注意,我没有获取内存中的所有数据文件,因为它需要更多内存。

2 个答案:

答案 0 :(得分:5)

File.ReadLines在内部创建ReadLinesIterator,在您枚举行时使用StreamReader.ReadLine()逐行读取文件:

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;
    }
}

因此,差异在于 - StreamReader.ReadLine()从流中读取单行。 File.ReadLines遍历所有行(直到您停止)并使用StreamReader.ReadLine()从流中读取每一行。

答案 1 :(得分:-1)

ReadAllLines一次读取文件中的所有行。 StreamReaders ReadLine逐行读取它,但您必须自己遍历文件,直到没有更多行要读取。 当你读到某些东西时......无论如何它都会在记忆中。