27.StreamReader和StreamWirter

使用StreamReader读取文本文件

namespace Demo {

    class Program {

        static void Main(string[] args) {

            //使用StreamReader来读取一个文本文件
            using (StreamReader sr = new StreamReader(@"C:\Users\22053\Desktop\new.txt", Encoding.Default)) {
                while (!sr.EndOfStream) {
                    Console.WriteLine(sr.ReadLine());
                }
            }
 
            Console.ReadKey();

        }


    }

}

详情请参考官方文档

使用StreamWriter写入文本文件

namespace Demo {

    class Program {

        static void Main(string[] args) {

            //使用StreamWriter来写入一个文本文件
            using (StreamWriter sw = new StreamWriter(@"C:\Users\22053\Desktop\a.txt", true)) {
                sw.Write("看我把你覆盖掉");
            }
            Console.WriteLine("OK");
            Console.ReadKey();

        }


    }

}

详情请参考官方文档

猜你喜欢

转载自www.cnblogs.com/lz32158/p/12920318.html
27