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();
}
}