[英]StreamReader to Read Range of lines
Lets say I have a File and want to read the lines, it is: 让我说我有一个文件,想要阅读这些行,它是:
while( !streamReader.EndOfStream ) {
var line = streamReader.ReadLine( );
}
How do I read only a range of lines? 我如何只读取一系列行? Like readlines from 10 to 20 only.
像只有10到20的读数线。
I suggest using Linq without any reader: 我建议没有任何读者使用Linq :
var lines = File
.ReadLines(@"C:\MyFile.txt")
.Skip(10) // skip first 10 lines
.Take(10); // take next 20 - 10 == 10 lines
...
foreach(string line in lines) {
...
}
In case you have to use the reader, you can implement something like this 如果你必须使用阅读器,你可以实现这样的事情
// read top 20 lines...
for (int i = 0; i < 20 && !streamReader.EndOfStream; ++i) {
var line = streamReader.ReadLine();
if (i < 10) // ...and skip first 10 of them
continue;
//TODO: put relevant code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.