C# 读写文件
2021-04-21 23:27
标签:读写文件 读取 mode iter ade stream def console 写文件 2、写文件 C# 读写文件 标签:读写文件 读取 mode iter ade stream def console 写文件 原文地址:http://blog.51cto.com/13567390/2058914
按行读取文件:
public void Read(string path)
{
StreamReader sr = new StreamReader(path,Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line.ToString());
}
}
public void Write(string path)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write("Hello World!!!!");
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
下一篇:C#质因子(自己别扭的逻辑。。)