StreamReader 读取文本流问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 21:22:14
class Exstream
{
public static string Read(string file)
{
string line="";
try
{
StreamReader sr = new StreamReader(file);
while ((line = sr.ReadLine()) != null) ;
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return line;
}
}
}

private void button2_Click(object sender, EventArgs e)
{
String file = @"G:\file.txt";
richTextBox1.Text = ExStream.Read(file);
}

代码段貌似没什么问题吧 可读取字符在 richTextBox1 中不显示出来
求助各大虾解惑....

这么写就行了
StreamReader sr = new StreamReader("d:\\AAAA.txt",Encoding.Default);
string line = "";
line = sr.ReadToEnd();
sr.Close();
sr.Dispose();
richTextBox1.Text = line;

while ((line = sr.ReadLine()) != null) ;
sr.Close();
修改为:
while ((line = sr.ReadLine()) == null)
sr.Close();