如何使用 Stream Reader 读取文本文件时跳过行数?

问题描述 投票:0回答:6

我有一个程序可以读取文本文件并将其处理成多个部分。

如何更改程序,让程序在使用Stream Reader读取文件时跳过读取文件的前5行?

有人可以建议吗?

代码:

class Program
{
    static void Main(string[] args)
    {
        TextReader tr = new StreamReader(@"C:\Test\new.txt");

        String SplitBy = "----------------------------------------";

        // Skip first 5 lines of the text file?
        String fullLog = tr.ReadToEnd();

        String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);

        //String[] lines = sections.Skip(5).ToArray();

        foreach (String r in sections)
        {
            Console.WriteLine(r);
            Console.WriteLine("============================================================");
        }
    }
}
c# line streamreader
6个回答
28
投票

尝试以下方法

// Skip 5 lines
for(var i = 0; i < 5; i++) {
  tr.ReadLine();
}

// Read the rest
string remainingText = tr.ReadToEnd();

13
投票

如果线路是固定的,那么最有效的方法如下:

using( Stream stream = File.Open(fileName, FileMode.Open) )
{
    stream.Seek(bytesPerLine * (myLine - 1), SeekOrigin.Begin);
    using( StreamReader reader = new StreamReader(stream) )
    {
        string line = reader.ReadLine();
    }
}

如果行的长度不同,那么您必须一次一行地阅读它们,如下所示:

using (var sr = new StreamReader("file"))
{
    for (int i = 1; i <= 5; ++i)
        sr.ReadLine();
}

8
投票

如果您想在程序中更多地使用它,那么创建一个继承自 StreamReader 并具有跳行功能的自定义类可能是个好主意。

类似这样的事情可以做到:

class SkippableStreamReader : StreamReader
{
    public SkippableStreamReader(string path) : base(path) { }

    public void SkipLines(int linecount)
    {
        for (int i = 0; i < linecount; i++)
        {
            this.ReadLine();
        }
    }
}

在此之后,您可以使用 SkippableStreamReader 的函数来跳行。 示例:

SkippableStreamReader exampleReader = new SkippableStreamReader("file_to_read");

//do stuff
//and when needed
exampleReader.SkipLines(number_of_lines_to_skip);

6
投票

我将在列表中添加另外两个建议。

如果总是有一个文件,而你只会阅读,我建议这样做:

var lines = File.ReadLines(@"C:\Test\new.txt").Skip(5).ToArray();

File.ReadLines 不会阻止其他文件,只会将必要的行加载到内存中。

如果您的流可以来自其他来源,那么我建议采用这种方法:

class Program
{
    static void Main(string[] args)
    {
        //it's up to you to get your stream
        var stream = GetStream();

        //Here is where you'll read your lines. 
        //Any Linq statement can be used here.
        var lines = ReadLines(stream).Skip(5).ToArray();

        //Go on and do whatever you want to do with your lines...
    }
}

public IEnumerable<string> ReadLines(Stream stream)
{
    using (var reader = new StreamReader(stream))
    {
        while (!reader.EndOfStream)
        {
            yield return reader.ReadLine();
        }
    }
}

使用完毕后,迭代器块将自动清理自身。 这里是乔恩·斯基特(Jon Skeet)撰写的一篇文章,深入探讨了它的工作原理(向下滚动到“最后......”部分)。


1
投票

我猜这很简单:

    static void Main(string[] args)
    {
        var tr = new StreamReader(@"C:\new.txt");

        var SplitBy = "----------------------------------------";

        // Skip first 5 lines of the text file?
        foreach (var i in Enumerable.Range(1, 5)) tr.ReadLine();
        var fullLog = tr.ReadToEnd(); 

        String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);

        //String[] lines = sections.Skip(5).ToArray();

        foreach (String r in sections)
        {
            Console.WriteLine(r);
            Console.WriteLine("============================================================");
        }
    }

1
投票

带有

StreamReader
ReadLine
ReadToEnd
实际上会将字节读取到内存中,即使您不处理这些行,它们也会被加载,这会在大文件的情况下影响应用程序性能( 10+ MB)。

如果您想跳过特定行数,您需要知道要移动到的文件的位置,这为您提供了两个选项:

  1. 如果您知道线的长度,您可以计算位置并使用 Stream.Seek 移动到那里。这是跳过流内容而不阅读它的最有效方法。这里的问题是你很少知道线的长度。
var linesToSkip = 10;
using(var reader = new StreamReader(fileName) )
{
    reader.BaseStream.Seek(lineLength * (linesToSkip - 1), SeekOrigin.Begin);
    var myNextLine = reader.ReadLine();
    // TODO: process the line
}
  1. 如果您不知道行长,则必须逐行阅读并跳过它们,直到到达所需的行号。这里的问题是行数很高,你会受到性能影响
var linesToSkip = 10;
using (var reader = new StreamReader(fileName))
{
    for (int i = 1; i <= linesToSkip; ++i)
        reader.ReadLine();

    var myNextLine = reader.ReadLine();
    // TODO: process the line
}

如果您需要跳过所有内容,则应该在不将所有内容读入内存的情况下执行此操作:

using(var reader = new StreamReader(fileName) )
{
   reader.BaseStream.Seek(0, SeekOrigin.End);

   // You can wait here for other processes to write into this file and then the ReadLine will provide you with that content

   var myNextLine = reader.ReadLine();
   // TODO: process the line
}
© www.soinside.com 2019 - 2024. All rights reserved.