首页 > Windows开发 > 详细

C#读写txt文件

时间:2017-05-15 21:47:20      阅读:301      评论:0      收藏:0      [点我收藏+]

最简单的读写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”目录下。

 

C#读写txt文件

原文:http://www.cnblogs.com/xbblogs/p/6858354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!