Resource

  • 리소스를 읽어오는 기능을 제공하는 인터페이스.
  • ApplicationContext extends ResourceLoader
@Component
@RequiredArgsConstructor
public class AppRunner implements ApplicationRunner {

    private final ResourceLoader resourceLoader;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        Resource resource = resourceLoader.getResource("classpath:test.txt");
        System.out.println("resource = " + resource.exists());

        System.out.println("=======Files.readString()==================");
        System.out.println(Files.readString(Path.of(resource.getURI())));
        System.out.println("===========================================");


        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        System.out.println(br.readLine());
    }
  • resources 폴더 밑에 "test.txt"파일이 위치한다.

+ Recent posts