상세 컨텐츠

본문 제목

[스트림] txt를 자바로 불러오기

자바

by esoesmio 2023. 4. 6. 23:00

본문

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) throws IOException{
        System.out.println("Hello world!");
//        FileReader reader = new FileReader("/Users/eunsukkim/Desktop/text.txt");

        try {
//            Path path = Paths.get(Main.class.getResource("hyundaicar.txt").toURI());
            Path path = Paths.get("/Users/eunsukkim/Desktop/text.txt");

            Stream<String> filestream = Files.lines(path, Charset.defaultCharset());

            filestream.forEach(l->System.out.println(l));
        }
//        catch (URISyntaxException ue) {
//            ue.printStackTrace();
//
//        }
        catch (IOException ie) {
            ie.printStackTrace();
        } catch(Exception e){

            e.printStackTrace();
        }
    }
}//            Path path = Paths.get(Main.class.getResource("hyundaicar.txt").toURI());
// 텍스트 파일을 행으로 분리해서 행들의 스트림을 만들어준다.

관련글 더보기

댓글 영역