using System.IO;
using System.Collections.Generic; // list에 값 저장하기 위해 사용
한 줄씩 읽는 방법
public void ConsoleRead()
{
List<string> list = new List<string>();
if (File.Exists(path))
{
using (var reader = new StreamReader(path))
{
//EndOfStream 속성을 이용하여 파일의 마지막행까지 반복
while (!reader.EndOfStream)
{
//ReadLine 메서드로 한 줄씩 읽기
string line = reader.ReadLine();
// 결과값 리스트에 저장
list.Add(line);
// 콘솔에 출력
Console.WriteLine(line);
}
}
}
else
{
Console.WriteLine("The file does not exist.");
}
}
콘솔 결과
텍스트 파일 전체 읽는 방법
public void ConsoleAllRead()
{
if (File.Exists(path))
{
using (var reader = new StreamReader(path))
{
string readData = File.ReadAllText(path);
Console.WriteLine(readData);
}
}
else
{
Console.WriteLine("The file does not exist.");
}
}