我有一个程序,它读取文本文件并处理它以分成几个部分。
所以问题是如何更改程序以允许程序在使用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("============================================================");
}
}
}
请尝试以下方法
// Skip 5 lines
for(var i = 0; i < 5; i++) {
tr.ReadLine();
}
// Read the rest
string remainingText = tr.ReadToEnd();
如果线路是固定的,那么最有效的方法如下:
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();
}
如果你想在程序中多次使用它,那么从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);
我将在列表中添加两个建议。
如果总有一个文件,你只会阅读,我建议:
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();
}
}
}
一旦完成,Iterator块将自动清理。 Here是Jon Skeet的一篇文章,深入探讨了它的工作原理(向下滚动到“最后......”部分)。
我猜它就像这样简单:
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("============================================================");
}
}
带有StreamReader
或ReadLine
的ReadToEnd
实际上会将字节读入内存,即使您没有处理这些行,它们也会被加载,这会影响大文件(10+ MB)时的应用程序性能。
如果要跳过特定数量的行,则需要知道要移动到的文件的位置,这为您提供了两个选项:
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
}
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
}