C# StreamReader.Readline() 出现一次读取多行怎么办

2025-02-26 18:42:38
推荐回答(2个)
回答1:

StreamReader sr = new StreamReader("data.txt", Encoding.Default);
//记得编码,不然读取汉字时会出错的
string text = sr.ReadToEnd();

回答2:

string[] str= File.ReadAllLines("abc.txt");
//如果一定要用StreamReader可以这样写
using (StreamReader sr = new StreamReader("abc.txt"))
{
 while (sr.Peek() >= 0) 
 {
 Console.WriteLine(sr.ReadLine()); 
 }
}