c# 读取txt文档和写入文档的方法


StreamReader sr = new StreamReader(path); //path是要读取的文件的完整路径

String str_read = sr.ReadToEnd();  //从开始到末尾读取文件的所有内容,str_read 存放的就是读取到的文本
sr.Close();  //读完文件记得关闭流
 
如果要一条一条读

while ((content = sr.ReadLine()) != null)//按行输出
{
f+=content;
}

写入文档方法

//FileMode.Append为不覆盖文件效果.create为覆盖
FileStream fs = new FileStream(path, mode: FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入

sw.Write(“xxxxx”);
//清空缓冲区
sw.Flush();
//关闭
sw.Close();
fs.Close();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM