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.