콘텐츠로 건너뛰기
Home » Process text file per line

Process text file per line

대용량 텍스트 파일을 한줄씩 읽기 – StreamReader를 작성하자

특정한 텍스트 파일을 읽어들여서 한 줄씩 처리하는 방법에서 가장 간단한 구현은 Stringinit(contentOfURL:encoding:)을 이용하여 텍스트 파일의 내용 전체를 하나의 문자열로 만든 다음에, 개행 문자를 이용해서 자르는 것이다.

let path = "~/Downloads/sample.txt"
let url = URL(fileURLWithPath: (NSString(string:path).expandingTildeInPath) // #1, #2
if let s = String(contentsOf: url) {
    for line in s.components(separatedBy:.newlines) {
        print(line)
    }
}

더 보기 »대용량 텍스트 파일을 한줄씩 읽기 – StreamReader를 작성하자