如何使用 Stream Reader 读取文本文件时跳过行数?
我有一个程序可以读取文本文件并将其处理为分成几个部分。
如何更改程序以允许程序在使用 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("============================================================");
}
}
}
I have a program which reads a text file and processes it to be separated into sections.
How can the program be changed to allow the program to skip reading the first 5 lines of the file while using the Stream Reader to read the file?
The code:
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("============================================================");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试以下操作
Try the following
如果行是固定的,那么最有效的方法如下:
如果行的长度不同,那么您必须一次一行地读取它们,如下所示:
If the lines are fixed then the most efficient way is as follows:
And if the lines vary in length then you'll have to just read them in a line at a time as follows:
如果您想在程序中多次使用它,那么最好创建一个继承自 StreamReader 的自定义类,并具有跳行功能。
可以这样做:
之后您可以使用 SkippableStreamReader 的函数来跳过行。
例子:
If you want to use it more times in your program then it maybe a good idea to make a custom class inherited from StreamReader with the ability to skip lines.
Something like this could do:
after this you could use the SkippableStreamReader's function to skip lines.
Example:
我将在列表中添加另外两个建议。
如果总是有一个文件,并且您只会阅读,我建议这样做:
File.ReadLines 不会阻止其他文件,只会加载到内存中必要的行。
如果您的流可以来自其他来源,那么我建议采用这种方法:
迭代器块将在您完成后自动清理。 这里是 Jon Skeet 撰写的一篇文章,深入探讨了它的工作原理(向下滚动到“最后......”部分)。
I'll add two more suggestions to the list.
If there will always be a file, and you will only be reading, I suggest this:
File.ReadLines doesn't block the file from others and only loads into memory necessary lines.
If your stream can come from other sources then I suggest this approach:
The Iterator block will automatically clean itself up once you are done with it. Here is an article by Jon Skeet going in depth into how that works exactly (scroll down to the "And finally..." section).
我猜这很简单:
I'd guess it's as simple as:
带有
ReadLine
或ReadToEnd
的StreamReader
实际上会将字节读取到内存中,即使您没有处理这些行,它们也会已加载,这会在大文件(10+ MB)的情况下影响应用程序性能。如果您想跳过特定行数,您需要知道要移动到的文件的位置,这为您提供了两个选择:
,如果你需要跳过所有内容,你应该在不将所有内容读入内存的情况下执行此操作:
The
StreamReader
withReadLine
orReadToEnd
will actually go and read the bytes into the memory, even if you are not processing these lines, they will be loaded, which will affect the app performance in case of big files (10+ MB).If you want to skip a specific number of lines you need to know the position of the file you want to move to, which gives you two options:
And if you need just skip everything, you should do it without reading all the content into memory: