C#如何跳过线的数量,同时使用读取流读取文本文件? [英] C# How to skip number of lines while reading text file using Stream Reader?
本文介绍了C#如何跳过线的数量,同时使用读取流读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个程序,读取文本文件和处理它被分隔为几个部分。
所以现在的问题是如何在程序改为允许程序跳过,而使用Stream Reader来阅读本文件中读取前5行的文件?
能否某人请告知代码? !谢谢
该代码:
类节目
{
静态无效的主要(字串[] args)
{
的TextReader TR =新的StreamReader(@C:\Test\\\
ew.txt);
字符串SplitBy =-------------------------------------- - ;
//跳过第一5行的文本文件?
字符串fullLog = tr.ReadToEnd();
的String [] =节fullLog.Split(新的String [] {} SplitBy,StringSplitOptions.None);
//的String [] =行sections.Skip(5).ToArray();
的foreach(节串R)
{
Console.WriteLine(R);
Console.WriteLine(========================================= ===================);
}
}
}
解决方案
请尝试以下
//跳过了(VAR 5系
I = 0; I< 5,我++){$ b $ b tr.ReadLine();
}
//阅读其余
串remainingText = tr.ReadToEnd();
I have a program which reads a text file and processes it to be seperated into sections.
So the question is 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?
Could someones please advise on the codes? Thanks!
The Codes:
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("============================================================");
}
}
}
解决方案
Try the following
// Skip 5 lines
for(var i = 0; i < 5; i++) {
tr.ReadLine();
}
// Read the rest
string remainingText = tr.ReadToEnd();
这篇关于C#如何跳过线的数量,同时使用读取流读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文