的TextReader和PEEK下一行 [英] TextReader and peek next line

查看:124
本文介绍了的TextReader和PEEK下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一个文本文件中使用的数据的TextReader

I read a data from a text file using TextReader

TextReader reader = new StreamReader(stream);
string line;
while ((line = reader.ReadLine()) != null)
 {
   //.......
 }

有时候,我需要偷看下一行(或几下一行)的读者。

我该怎么办呢?

推荐答案

编辑:更新,允许任意数量的偷窥的:

Updated to allow any number of peeks:

public class PeekingStreamReader : StreamReader
{
    private Queue<string> _peeks;

    public PeekingStreamReader(Stream stream) : base(stream)
    {
        _peeks = new Queue<string>();   
    }

    public override string ReadLine()
    {
        if (_peeks.Count > 0)
        {
            var nextLine = _peeks.Dequeue();
            return nextLine;
        }
        return base.ReadLine();
    }

    public string PeekReadLine()
    {
        var nextLine = ReadLine();
        _peeks.Enqueue(nextLine);
        return nextLine;
    }
}

这篇关于的TextReader和PEEK下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆