ReadAllLines() 메서드를 사용하여 파일의 모든 줄을 한 줄씩 읽습니다.
다음 줄이 포함된 "new.txt" 파일이 있다고 가정해 보겠습니다.
One Two Three
먼저 읽을 파일의 경로를 설정합니다.
String myPath = "new.txt";
이제 문자열 배열 아래에 추가하여 한 줄씩 가져옵니다.
String[] fLine = File.ReadAllLines(myPath);
첫 번째 줄을 가져와야 한다고 가정해 보겠습니다. 그러려면.
fLine[0]
다음은 파일에서 한 줄씩 읽는 완전한 예입니다.
예
using System;
using System.IO;
public class Demo {
public static void Main() {
String myPath = "new.txt";
String[] fLine;
// array of lines in a file
fLine = File.ReadAllLines(myPath);
// read lines of a file
Console.WriteLine("Line 1: "+fLine[0]);
Console.WriteLine("Line 2: "+fLine[1]);
Console.WriteLine("Line 3: "+fLine[2]);
Console.WriteLine("Line 4 "+fLine[3]);
}
} 출력
Line1: One Line2: Two Line3: Three Line4: Four