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.