读写文本

1.读文件

//方法1
StreamReader srFile=new StreamReader(filename,Encoding.Default);
while(srFile.Peek() > -1) // Check EOF
{
sql+= srFile.ReadLine(); // Read one line
}
srFile.Close();
this.richTextBox1.Text=sql;

//方法2
StreamReader sr=File.OpenText(filename);
String input;
while((input=sr.ReadLine())!=null)
{
    sql+=input;
}
sr.Close();
this.richTextBox1.Text=sql;

//方法3
this.richTextBox1.LoadFile(filename,RichTextBoxStreamType.PlainText);

2.写文件

StreamWriter sw=new StreamWriter(filename);//,false);
sw.Write(text);
sw.Flush();//从缓冲区写入基础流(文件)
sw.Close();
Contributors: FHL