An array of streams in C#

CsharpProgrammingServer Side Programming

Set the string array for the values −

string[] names = new string[] {"Jack", "Tom"};

Now using foreach array, write the content in the file −

using (StreamWriter sw = new StreamWriter("names.txt")) {

   foreach (string s in names) {
      sw.WriteLine(s);
   }
}

The following is an example showing an array of streams to write text to a file −

Example

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();
      }
   }
}
raja
Updated on 21-Jun-2020 14:44:13

Advertisements