读取文件中的特定行 [英] Reading specific line in file
问题描述
如何从文件中读取一行?当我想写文件时,只需创建FileStream和FileWriter并写一行.但是,如果我想阅读一些特定的行怎么办?我注意到FileReader也存在,但没有发现如何使用它.
对于这样的菜鸟问题,我很抱歉,我仅几天就学习C#.感谢您的帮助.
好吧,它应该看起来像str = streamReader.ReadLine();.但是如何指定要阅读的行?找不到如何使用ReadLine和msdn的解释是不好的:(
Hi,
How to read one line from file? When I want to write to file its simply I just create FileStream and FileWriter and write line. But what if I want to read some specific line? I''ve noticed that FileReader exists too but didn''t found nowhere how to use it.
I''am sorry for such noob question I''am learning C# just few days. Thanks for any help.
Well so it should look like str = streamReader.ReadLine(); but how can I specify what line I want to read? Can''t find how to use ReadLine and msdn explanation is bad :(
推荐答案
要从文件/流中进行面向行的读取,请使用System.IO.StreamReader
:
http://msdn.microsoft.com/en-us/library/system.io. streamreader.aspx [ ^ ].
—SA
For line-oriented reading from file/stream, useSystem.IO.StreamReader
:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].
—SA
从文件读取行是线性的,仅向前的过程.换句话说,如果您想读取行号N,则读取行1到N-1(如果不需要则将它们丢弃),然后下一行将是行N.
Reading lines from a file is a linear, forward only process. In other words, if you want to read line number N, you read lines 1 through N-1 (and throw them away if you don''t need them) and then the next line read will be line N.
这篇关于读取文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!