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.