首頁  >  文章  >  後端開發  >  在C#中的一組流

在C#中的一組流

WBOY
WBOY轉載
2023-08-23 08:17:05701瀏覽

在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();
      }
   }
}

以上是在C#中的一組流的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除