本示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容读取(一次读取一行)到字符串中。所有文本行都保存在字符串 line 中并显示在屏幕上。
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
使用StreamReader读取文件
本文介绍了一种使用C#中的StreamReader类逐行读取文本文件的方法。通过实例演示了如何打开文件、读取每一行内容并在控制台上显示这些内容。
3581

被折叠的 条评论
为什么被折叠?



