Spring 외부 리소스 파일 읽기(Resource)

어겐어갠·2022년 4월 6일
0

Resource와 ResourceLoader 인터페이스로 외부 리소스 파일을 읽을 수 있다.

Resource resource = applicationContext.getResource("file:sample.txt");
System.out.println("resource = " + resource.getClass().getCanonicalName());
File file = resource.getFile();
List<String> strings = Files.readAllLines(file.toPath());
logger.info("strings = " + strings.stream().reduce("", (a, b) -> a + "\n" + b));
Resource resource = applicationContext.getResource("https://stackoverflow.com/");
ReadableByteChannel readableByteChannel = Channels.newChannel(resource.getURL().openStream());
String collect = new BufferedReader(Channels.newReader(readableByteChannel, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
logger.info("collect = " + collect);
profile
음그래

0개의 댓글