CHIP KIDD

[C#] 5. 파일처리(텍스트파일) 본문

전기전자/C# 시각화프로그래밍

[C#] 5. 파일처리(텍스트파일)

쑨야미 2021. 4. 14. 19:02
using System;
using System.IO;

namespace Day02_02_파일처리
{
    class Program
    {
        static void Main(string[] args)
        {
            // *파일처리의 3단계
            // 파일 열기 --> 파일읽기/쓰기(반복) --> 파일 닫기
            // * 파일의 종류 : 텍스트 파일, 바이너리 파일
            // * 텍스트 파일 : 메모장에서 열어서 글자처럼 보임.
            //      --> 1바이트가 의미가 있음.
            // * 바이너리 : 비트가 의미. 2비트 색상, 3비트 크기,7 비트.....
            string fullName = "c:/temp/data1.txt";
            string writeName = "c:/temp/data2.txt";
            StreamReader sr
              = new StreamReader(new FileStream(fullName, FileMode.Open));
            StreamWriter sw
              = new StreamWriter(new FileStream(writeName, FileMode.Create));

            string line1;
            while(!sr.EndOfStream)
            {
                line1 = sr.ReadLine();
                Console.WriteLine(line1);
                sw.WriteLine(line1);
            }
            sr.Close();
            sw.Close();

        }
    }
}