1. 程式人生 > >檔案IO(存取.txt檔案)

檔案IO(存取.txt檔案)

//存檔案方法

 public void Save_File_Info(string Save_Path)

 {

      //根據路徑,建立檔案和資料流

            FileStream FS = new FileStream(Save_Path, FileMode.Create);

            StreamWriter SW = new StreamWriter(FS);

            //開始寫入

            SW.WriteLine(“寫入檔案內容”);

            //清空緩衝區

            SW.Flush();

            //關閉流

            SW.Close();

            FS.Close();

 }

//讀取檔案方法

 public void Read_File_Info(string Read_Path)

 {

            StreamReader SR = new StreamReader(Read_Path, false);

string str=“”;

            if ((str = SR.ReadLine()) != null)

            {

                    Console.WriteLine(str);

            }

   SR .Close();

 }

   注:path路徑為.txt檔案路徑,此方法適用整行資料流的讀取,可修改部分進行完善........