一行一行读取文件的两种方式

1)方式1

static void Main(string[] args)
        {
using (StreamReader reader = new StreamReader("1.txt", Encoding.Default))
            {
                //只读取了一行
                //string msg = reader.ReadLine();
                string msg;

                //一直读取到流的末尾
                while((msg=reader.ReadLine())!=null)//!=  不等于   
               {
                     Console.WriteLine(msg);
               }
                
            }
            Console.ReadKey();
        }

方式2

static void Main(string[] args)
        {
            using (StreamReader reader = new StreamReader("1.txt", Encoding.Default))
            {

                while (!reader.EndOfStream)
                {
                    Console.WriteLine(reader.ReadLine());
                }

            }
            Console.ReadKey();
        }

猜你喜欢

转载自blog.csdn.net/nsjlive/article/details/81155227
今日推荐