使用StreamReader和StreamWriter读写 txt file.
//读
public string ReadTxtFile(string path) //ReadAll("A:\\test.txt");
{
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
return (sr.ReadToEnd().ToString());
//while ((line = sr.ReadLine()) != null) 一行一行读
//{
// Console.WriteLine(line.ToString());
//}
}
}
//写
public void WriteToTxtFile(string path,string content)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//写入
sw.Write(content);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
c# 读写txt文件
最新推荐文章于 2024-09-14 01:32:23 发布