如何在textfile中再获取两行

时间:2016-04-25 10:28:48

标签: c#

我有以下代码,我想要这一行,接下来的2行如何做到这一点。请帮忙,我想要最后一个号码,请看下面的文字。

static void Main()
{
    int counter = 0; string line;
    StringBuilder sb = new StringBuilder();

    using (System.IO.StreamReader file = new System.IO.StreamReader(@"E:\file\log.txt"))
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(DateTime.Now.Date.ToShortDateString()))
            {         
                sb.AppendLine(line.ToString());
            }
        }
    }

    Console.WriteLine(sb.ToString());
    Console.ReadKey();
}

*****收到电话:25/04/2016 - 11:45 *****

NAME:测试

号码:(425)555-1212

*****收到电话:25/04/2016 - 11:45 *****

NAME:Test2

号码:(425)544-1213

2 个答案:

答案 0 :(得分:4)

只需再调用file.ReadLine两次,但重要的是检查返回的字符串是否为null,因为您可能会遇到意外的文件结尾,并且您的代码不再受while条件中的检查保护

...
using (System.IO.StreamReader file = new System.IO.StreamReader(@"E:\file\log.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains(DateTime.Now.Date.ToShortDateString()))
        {

            sb.AppendLine(line);
            line = file.ReadLine();
            if(line != null) sb.AppendLine(line);
            line = file.ReadLine();
            if(line != null) sb.AppendLine(line);
        }
    }
}

如果你想得到最后三行(假设你的文件格式正确),你可以在循环中添加StringBuilder的重置

using (System.IO.StreamReader file = new System.IO.StreamReader(@"E:\file\log.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains(DateTime.Now.Date.ToShortDateString()))
        {
            // This will remove the previous data and keep 
            // just the last three lines.....
            sb.Length = 0;
            sb.AppendLine(line);
            line = file.ReadLine();
            if(line != null) sb.AppendLine(line);
            line = file.ReadLine();
            if(line != null) sb.AppendLine(line);
        }
    }
}

另外,但从性能的角度来看,我不确定这是不是一个好主意(特别是如果我们谈论的是一个大文件),我们可以用这种方式使用linq

string result = string.Join(Environment.NewLine, 
                File.ReadLines(@"E:\file\log.txt").Reverse().Take(3));

答案 1 :(得分:0)

 static void Main()
        {
            int counter = 0; string line;

            List<string> ss = new List<string>();
            using (System.IO.StreamReader file = new System.IO.StreamReader(@"E:\file\log.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {

                    if (line.Contains(DateTime.Now.Date.ToShortDateString()))
                    {
                        ss.Add(line);                     
                        line = file.ReadLine();
                        if (line != null)

                            ss.Add(line);
                        line = file.ReadLine();
                        if (line != null)                           
                            ss.Add(line);

                    }
                }
            }

            var item = ss.LastOrDefault();            
            string number = item.Substring(0, 24).Replace("NUMBER:", "").Trim();
            Console.WriteLine(number);
            Console.ReadKey();
}