상식닷컴
로그인
가입하기
2026년 상식닷컴 선정 식당 & 카페 리스트
2025년 2026년 신상 호텔 리스트
최근에 오픈한 호텔을 찾는다면 살펴보세요
일주일 식단표 어플
자동 일주일 식단표 어플
안드로이드
아이폰
주식 & 코인 차트의 신
1000만원으로 2000만원 만들기 프로젝트
수정하기 - C#에서 파일 입출력(I/O)은 어떻게 하나요?
닉네임
비밀번호
제목
내용
[이미지 업로드는 권한이 있는 사람만 가능. 하단 카톡으로 연락]
C 에서 파일 입출력(I/O)은 다양한 방법으로 수행할 수 있으며, 주로 `System.IO` <a href='https://sangseek.com/sangseeks/네임/ko'>네임</a>스페이스에 포함된 클래스를 사용합니다. 이 네임스페이스는 파일 및 디렉터리 작업을 위한 다양한 기능을 제공합니다. 다음은 C 에서 파일 입출력을 수행하는 방법에 대한 자세한 설명입니다. 1. 파일 읽기 및 쓰기 1.1. <a href='https://sangseek.com/sangseeks/파일 쓰기/ko'>파일 쓰기</a> 파일에 데이터를 쓰기 위해서는 `StreamWriter` 클래스를 사용할 수 있습니다. 이 클래스는 텍스트 파일에 문자열을 작성하는 데 유용합니다. ```csharp using System; using System.IO; class Program { static <a href='https://sangseek.com/sangseeks/void/ko'>void</a> Main() { string filePath = "example.txt"; // StreamWriter를 사용하여 파일에 쓰기 using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine("Hello, World!"); writer.WriteLine("This is a test file."); } Console.WriteLine("파일에 데이터가 성공적으로 기록되었습니다."); } } ``` 위의 코드에서 `using` 블록을 사용하여 `StreamWriter` 객체를 생성했습니다. `using` 블록을 사용하면 작업이 끝난 후 자동으로 리소스가 <a href='https://sangseek.com/sangseeks/해제/ko'>해제</a>됩니다. 1.2. 파일 읽기 파일에서 데이터를 읽기 위해서는 `StreamReader` 클래스를 사용할 수 있습니다. ```csharp using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // StreamReader를 사용하여 파일 읽기 using (StreamReader reader = new StreamReader(filePath)) { string line; <a href='https://sangseek.com/sangseeks/while/ko'>while</a> ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } } ``` 위의 코드에서는 `StreamReader`를 사용하여 파일의 각 줄을 읽고 콘솔에 출력합니다. 2. 파일 존재 여부 확인 파일이 존재하는지 확인하려면 `File.Exists` 메서드를 사용할 수 있습니다. ```csharp string filePath = "example.txt"; if (File.Exists(filePath)) { Console.WriteLine("파일이 존재합니다."); } else { Console.WriteLine("파일이 존재하지 않습니다."); } ``` 3. 파일 복사, 이동 및 삭제 C 에서는 파일을 복사, 이동 및 삭제하는 데 사용할 수 있는 메서드가 제공됩니다. 3.1. 파일 복사 ```csharp string sourceFile = "example.txt"; string destinationFile = "example_copy.txt"; File.Copy(sourceFile, destinationFile, true); // true는 덮어쓰기를 의미 ``` 3.2. <a href='https://sangseek.com/sangseeks/파일 이동/ko'>파일 이동</a> ```csharp string sourceFile = "example.txt"; string destinationFile = "example_moved.txt"; File.Move(sourceFile, destinationFile); ``` 3.3. 파일 삭제 ```csharp string filePath = "example.txt"; if (File.Exists(filePath)) { File.Delete(filePath); Console.WriteLine("파일이 삭제되었습니다."); } ``` 4. 비동기 파일 I/O C 에서는 비동기 <a href='https://sangseek.com/sangseeks/파일 작업/ko'>파일 작업</a>을 위한 메서드도 제공합니다. `<a href='https://sangseek.com/sangseeks/FileStream/ko'>FileStream</a>` 클래스와 `StreamReader`, `StreamWriter`의 비동기 메서드를 사용할 수 있습니다. 4.1. 비동기 파일 쓰기 ```csharp using System; using System.IO; using System.Threading.Tasks; class Program { static async Task Main() { string filePath = "example_async.txt"; using (StreamWriter writer = new StreamWriter(filePath)) { await writer.WriteLineAsync("Hello, Async World!"); } Console.WriteLine("비동기 파일 쓰기가 완료되었습니다."); } } ``` 4.2. 비동기 파일 읽기 ```csharp using System; using System.IO; using System.Threading.Tasks; class Program { static async Task Main() { string filePath = "example_async.txt"; using (StreamReader reader = new StreamReader(filePath)) { string content = await reader.ReadToEndAsync(); Console.WriteLine(content); } } } ``` 5. 파일 및 디렉터리 작업 C 에서는 파일뿐만 아니라 디렉터리 작업도 쉽게 수행할 수 있습니다. `Directory` 클래스를 사용하여 디렉터리를 생성, 삭제, 이동 및 나열할 수 있습니다. 5.1. 디렉터리 생성 ```csharp string directoryPath = "example_directory"; if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); Console.WriteLine("디렉터리가 생성되었습니다."); } ``` 5.2. 디렉터리 삭제 ```csharp if (Directory.Exists(directoryPath)) { Directory.Delete(directoryPath, true); // true는 하위 디렉터리 및 파일을 포함하여 삭제 Console.WriteLine("디렉터리가 삭제되었습니다."); } ``` 5.3. 디렉터리 내용 나열 ```csharp string[] files = Directory.GetFiles(directoryPath); foreach (string file in files) { Console.WriteLine(file); } ``` 결론 C 에서 파일 입출력(I/O)은 `System.IO` 네임스페이스의 다양한 클래스를 통해 쉽게 수행할 수 있습니다. 파일을 읽고 쓰는 기본적인 방법부터 비동기 작업, 디렉터리 작업까지 다양한 기능을 제공하므로, 필요에 따라 적절한 방법을 선택하여 사용할 수 있습니다. 이러한 기능들을 활용하여 파일 기반의 애플리케이션을 개발하는 데 큰 도움이 될 것입니다.
이용안내
커뮤니티 이용안내
×
- 게시한 게시글로 발생하는 문제는 게시자에게 책임이 있습니다.
- 게시글이 타인/타업체의 저작권을 침해할 경우 모든 책임은 게시자에게 있습니다. 게시자가 모든 손해를 부담해야 합니다.
- 상식닷컴 운영자는 게시자와 상의하지 않고 게시글을 수정 또는 삭제할 수 있습니다.
- 상식닷컴 운영자는 깨끗한 커뮤니티 공간을 만드는 것이 1순위입니다.
수정하기
취소하기