最简单的读写txt文件方式
引用:
using System.IO;
using System.Text;
代码:
class Program { static void Main(string[] args) { string path ="test.txt"; Write(path); Console.WriteLine(Read(path)); Console.ReadKey(); } public static void Write(string path) { using (FileStream fs = new FileStream(path, FileMode.Append)) { StreamWriter sw = new StreamWriter(fs); sw.WriteLine("Hello World!!!!"); sw.WriteLine("Hello World!!!!"); sw.Flush(); Console.WriteLine("写入成功!"); } } public static string Read(string path) { StreamReader sr = new StreamReader(path, Encoding.Default); return sr.ReadToEnd(); } }
本程序为控制台程序,创建的test.txt文件在“inDebug”目录下。