일반 텍스트 파일을 열려면 StreamReader 클래스를 사용하십시오. 다음은 읽을 파일을 엽니다 -
StreamReader sr = new StreamReader("d:/new.txt") 이제 파일의 내용을 표시하십시오 -
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
} 다음은 코드입니다 -
예
using System;
using System.IO;
namespace FileApplication {
class Program {
static void Main(string[] args) {
try {
using (StreamReader sr = new StreamReader("d:/new.txt")) {
string line;
// Read and display lines from the file until
// the end of the file is reached.
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
} catch (Exception e) {
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
} 출력
The file could not be read: Could not find a part of the path "/home/cg/root/4281363/d:/new.txt".