본문 바로가기

C language

[C#] txt 파일 읽기,삭제,쓰기


실제예제




            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\test\candidate.txt", true))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    para[num] = Convert.ToInt16(line);
                    num++;
                }
            } ⁄⁄ txt 파일 읽어오기

            if (System.IO.File.Exists(@"C:\test\can1.txt"))
                System.IO.File.Delete(@"C:\test\can1.txt");
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\can1.txt", true))
            {
                foreach (int line in para)
                {
                    file.WriteLine(line);
                }
            }


예제 설명



System.IO.StreamReader 를 사용하여 특정 txt파일을 읽어온다.


그 후 line 과 sr.ReadLine() 을 비교하여서 null 일경우를 제외하고 계속 while 문을 수행한다.


그후 System.IO.File.Delete 를 이용하여 해당 경로에 존재하는 특정 txt 파일을 지울 수 있다.


System.IO.StreamWriter  로 다시 해당 txt 파일에 지정된 변수(para) 를 출력하는 예제이다.



'C language' 카테고리의 다른 글

[C#] txt 파일에 쓰기  (0) 2013.05.25
[C#] 구조체를 다차원배열로  (0) 2013.04.19