C#StreamReader與StreamWirter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _11StreamReader與StreamWirter
{
    class Program
    {
        static void Main(string[] args)
        {
            #region StreamReader
            //逐行讀取文本文件StreamReader
            string source = @"1.txt";
            using (StreamReader sr = new StreamReader(source, Encoding.Default))
            {
                //StreamReader是逐行的讀取文本
                //直到文件的末尾
                while (!sr.EndOfStream)
                {
                    Console.WriteLine(sr.ReadLine());
                }
               
            }
            #endregion


            #region StreamWriter
            //逐行寫入文本文件StreamWriter
            string target = @"2.txt";
            using (StreamWriter sw = new StreamWriter(target,true, Encoding.UTF8))
            {
                for (int i = 0; i < 100; i++)
                {
                    sw.WriteLine(i);
                }
                Console.WriteLine("=> WriteLine => OK");
            }
            #endregion
            Console.ReadKey();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章