C#基础 StreamReader ReadLine 读取txt文件的所有行

版权声明:知识来源于智慧与善良并存的师傅们,亦当回报给智慧与善良并存的人们。 https://blog.csdn.net/yushaopu/article/details/82312909

慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。


  •    .NET Framework : 4.6.1
  •                           ide : visual studio 2017 community
  •                             os : win7 x86_64
  •            type setting : markdown
  •                         blog : https://blog.csdn.net/yushaopu
  •                     github : https://github.com/GratefulHeartCoder

my.txt(utf8)

道德经·第十章
【作者】老子 【朝代】春秋时期
载营魄抱一,能无离乎?

专气致柔,能如婴儿乎?

涤除玄览,能无疵乎?

爱民治国,能无以智乎?

天门开阖,能为雌乎?

明白四达,能无为乎?

生之畜之,生而不有,为而不恃,长而不宰,是谓玄德。


来自:https://hanyu.baidu.com/s?wd=%E9%81%93%E5%BE%B7%E7%BB%8F%C2%B7%E7%AC%AC%E5%8D%81%E7%AB%A0&from=poem

code

using System;
using System.IO;
using System.Text;

namespace ConsoleApp
{

    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader sReader = new StreamReader(@"my.txt", Encoding.UTF8))
            {
                string aLine;
                // 控制while循环是否进行的变量,true打印文本,false跳出循环
                bool condition = true;

                while (true)
                {
                    aLine = sReader.ReadLine();

                    // aline=null -> 文本读完了,那么控制量condition结合if语句 跳出循环                 
                    // 如果文本没有读完,那么condition结合if语句的作用就是输出读到的文本
                    if (aLine == null)
                    {
                        condition = false;
                    }

                    if (condition)
                    {
                        Console.WriteLine(aLine);
                    }
                    else
                    {
                        break;
                    }
                }

            }
            Console.ReadKey();
        }
    }
}

result

道德经·第十章
【作者】老子 【朝代】春秋时期
载营魄抱一,能无离乎?

专气致柔,能如婴儿乎?

涤除玄览,能无疵乎?

爱民治国,能无以智乎?

天门开阖,能为雌乎?

明白四达,能无为乎?

生之畜之,生而不有,为而不恃,长而不宰,是谓玄德。


来自:https://hanyu.baidu.com/s?wd=%E9%81%93%E5%BE%B7%E7%BB%8F%C2%B7%E7%AC%AC%E5
%8D%81%E7%AB%A0&from=poem

resource


感恩曾经帮助过 心少朴 的人。
C#优秀,值得学习。.NET Core具有跨平台的能力,值得关注。
Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d,UWP可以适当地了解。
注:此文是自学笔记所生,质量中下等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。

猜你喜欢

转载自blog.csdn.net/yushaopu/article/details/82312909
今日推荐