C#读写文本文件源码片段
程序员文章站
2024-03-17 11:11:46
...
下边内容段是关于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();
}
}