C#读写txt文件

最简单的读写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文件在“\bin\Debug”目录下。

 

posted @ 2017-05-15 20:52  .追风逐月  阅读(753)  评论(0编辑  收藏  举报