问题描述
我是一个初学者。
我需要逐行读取TXT文件
怎么做?
I am a beginer.
I need to read a TXT file line by line
How to do it?
推荐答案
查找StreamReader及其ReadLine方法。
谢谢,
Seth Rowe
Look up the StreamReader and it''s ReadLine method.
Thanks,
Seth Rowe
尝试这样的事情:
static void Main()
{
FileStream fileStream = null;
StreamReader streamReader = null;
试试
{
fileStream = new FileStream(@" c:\foo.txt",FileMode.Open);
streamReader = new StreamReader(fileStream);
while(true )
{
string line = streamReader.ReadLine();
if(string.IsNullOrEmpty(line))
休息;
}
}
终于
{
if( streamReader!= null)
streamReader.Close();
if(fileStream!= null)
fileStream.Close();
}
}
希望它有所帮助,
John
Hi, try something like this:
static void Main()
{
FileStream fileStream = null;
StreamReader streamReader = null;
try
{
fileStream = new FileStream(@"c:\foo.txt", FileMode.Open);
streamReader = new StreamReader(fileStream);
while (true)
{
string line = streamReader.ReadLine();
if (string.IsNullOrEmpty(line))
break;
}
}
finally
{
if (streamReader != null)
streamReader.Close();
if (fileStream != null)
fileStream.Close();
}
}
Hope it helps,
John
这篇关于逐行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!