C#读写文本文件源码片段
下边内容段是关于C#读写文本文件片段的内容,应该是对码农们也有用。
using System;
using System.IO;
public class TestReadFile
{
public static void Main(String[] args)
{
FileStream fs = new FileStream(@c:temptest.txt , FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
String line=sr.ReadLine();
while (line!=null)
{
Console.WriteLine(line);
line=sr.ReadLine();
}
sr.Close();
fs.Close();
}
}
写文本文件代码片段
using System;
using System.IO;
public class TestWriteFile
{
public static void Main(String[] args)
{
FileStream fs = new FileStream(@c:temptest.txt , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine( First Line );
sw.WriteLine( Second Line);
sw.Flush();
}
}
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步