StreamReader和StreamWriter

FileStream 是操作字节的

StreamReader跟StreamWriter是操作字符串的

操作文件的方法的命名空间都是IO

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StreamReader跟StreamWriter
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用StreamReader来读取一个文件
            using (StreamReader sr = new StreamReader(@"F:\程序测试文件夹\aoe.txt",Encoding.UTF8))
            {
                while(!sr.EndOfStream )
                {
                    Console.WriteLine(sr.ReadLine());
                }
                Console.ReadKey();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StreamReader跟StreamWriter
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用StreamReader来读取一个文件
            //using (StreamReader sr = new StreamReader(@"F:\程序测试文件夹\aoe.txt",Encoding.UTF8))
            //{
            //    while(!sr.EndOfStream )
            //    {
            //        Console.WriteLine(sr.ReadLine());
            //    }
            //    Console.ReadKey();
            //}

            //使用StreamWriter来写入一个文本文件
            using (StreamWriter sw = new StreamWriter(@"F:\程序测试文件夹\qwer.txt"))
            {
                sw.WriteLine("爱丽丝凯迪拉克金克拉撒旦发刻录机啊圣诞快乐就奥克兰就看来简洁大方深刻理解上的法");

            }
            Console.WriteLine("ok");
            Console.ReadKey();


        }
    }
}

 

这种写入都是会把之前的覆盖掉的,要想不覆盖,需要在创建StreamWriter对象的时候,重载里面有个bool Append,来确认是否追加,如果是追加那就填个true,不然就默认覆盖

 

posted @ 2021-07-18 13:33  静态类  阅读(77)  评论(0编辑  收藏  举报