設定字串陣列的值−
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中文網其他相關文章!