• C#中使用StreamReader实现文本文件的读取与写入


    场景

    实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。

    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    大量编程视频教程:https://space.bilibili.com/164396311

    实现

    文本文件读取

    新建命令窗口程序。

    在main方法中:

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

    在上面的代码中读取了指定文件路径和指定了编码格式的文本文件。

    效果

    文本文件写入

     //使用StreamWriter来写入一个文本文件
                using (StreamWriter sw = new StreamWriter(@"C:UsersAdministratorDesktop
    ew.txt"))
                {
                    sw.Write("关注公众号:霸道的程序猿,获取编程相关电子书、教程推送与免费下载。大量编程视频教程:https://space.bilibili.com/164396311");
                }
                Console.WriteLine("写入成功");
                Console.ReadKey();

    效果

  • 相关阅读:
    简易自制线程池(备忘)
    大数据量的删除过程查看
    收集书籍备忘
    6月12日C代码
    fseek()
    区分int *p[4]与int (*p)[4]
    常用的字符串处理函数 C语言
    6月11日
    C学习代码
    文件读取 C语言
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/11456103.html
Copyright © 2020-2023  润新知