第12节:C# 文本文件的读写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haibing_zhao/article/details/81464045

代码:

/****************************
 * 
 * 纯文本文件的读写
 * StreamReader,StreamWriter
 * 
 * ***************************/


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace 文件流技术
{
    class Demo3
    {
        private string _StrAimPath = @"E:\TeacherLiuSutdy\Hight Level Teach\Test3.txt";
        /// <summary>
        /// 文本文件的写入
        /// </summary>
        public void Test1()
        {
            string str = "同学大家上午好";
            StreamWriter sw = new StreamWriter(_StrAimPath);
            sw.WriteLine(str);
            sw.Close();
        }
        /// <summary>
        /// 文本文件的读取
        /// </summary>
        public void Test2()
        {
            StreamReader sr = new StreamReader(_StrAimPath);
            while (sr.Peek()>=0)
            {
                Console.WriteLine(sr.ReadLine());
            }
            sr.Close();
        }
        static void Main(string[] args)
        {
            Demo3 obj = new Demo3();
            obj.Test1();
            obj.Test2();
            Console.ReadKey();
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/haibing_zhao/article/details/81464045