C# 中的流陣列


設定值的字串陣列 −

string[] names = new string[] {"Jack", "Tom"};

現在使用 foreach 陣列,在檔案中寫入內容 −

using (StreamWriter sw = new StreamWriter("names.txt")) {

   foreach (string s in names) {
      sw.WriteLine(s);
   }
}

以下是顯示流陣列以向檔案寫入文字的示例 −

例項

using System;
using System.IO;

namespace FileApplication {
   class Program {
      static void Main(string[] args) {
         string[] names = new string[] {"Jack", "Tom"};

         using (StreamWriter sw = new StreamWriter("names.txt")) {

            foreach (string s in names) {
               sw.WriteLine(s);
            }
         }

         // Read and show each line from the file.
         string line = "";
         using (StreamReader sr = new StreamReader("names.txt")) {
            while ((line = sr.ReadLine()) != null) {
               Console.WriteLine(line);
            }
         }
         Console.ReadKey();
      }
   }
}

更新於: 21-Jun-2020

592 次瀏覽

開啟您的 職業 生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.