readline然后将指针移回?

Yan*_*ang 5 c# file-io readline

是否有一个函数streamreader允许peek/read下一行获取更多信息而不实际将迭代器移动到下一个位置?

当前行的操作取决于下一行,但我想保持使用此代码块的完整性

while((Line = sr.ReadLine())!=null)
Run Code Online (Sandbox Code Playgroud)

var*_*bas 9

原则上,没有必要做这样的事情.如果要关联两个连续的行,只需使分析适应这一事实(在读取第2行时执行第1行操作).示例代码:

using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
{
    string line = null;
    string prevLine = null;
    while ((line = sr.ReadLine()) != null)
    {
        if (prevLine != null)
        {
            //perform all the actions you wish with the previous line now
        }

        prevLine = line;
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能适合于根据需要处理尽可能多的行(前一行而不是前一行的集合prevLine).

  • @Yang(我选择了一个好名字然后:))如果一切正常,为什么要改变呢?我经常使用这种方法,从来没有遇到过问题:它可靠,清晰,快速. (2认同)