Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

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