Spring BootでResourceLoaderを使用してresources配下のファイルを扱う

Spring BootでResourceLoaderを使用してresources配下のファイルを扱う

ResourceLoaderインタフェースを使用してsrc/main/resources配下のファイルを読み込みます。

a.txt

resouces/a.txt

aaa
bbb
ccc

3行のa.txtを読み込みます。

import

ResourceとResourceLoaderをimportします。

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

classpath

classpath:に続けて記述します。

Resource resource = resourceLoader.getResource("classpath:a.txt");

全体的なファイルです。

package jp.co.confrage.demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

@SpringBootApplication
public class DemoApplication {
    private static ResourceLoader resourceLoader;
    DemoApplication(ResourceLoader resourceLoader) {
        DemoApplication.resourceLoader = resourceLoader;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        Resource resource = resourceLoader.getResource("classpath:a.txt");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {}
    }

}

テスト

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.3)

2024-03-21T14:28:20.571+09:00  INFO 5564 --- [demo] [           main] jp.co.confrage.demo.DemoApplication      : Starting DemoApplication using Java 21.0.1 with PID 5564 (C:\demo\build\classes\java\main started by takahashi-h5 in C:\xxxxxxx)
2023-03-21T19:28:20.575+09:00  INFO 5564 --- [demo] [           main] jp.co.confrage.demo.DemoApplication      : No active profile set, falling back to 1 default profile: "default"
2023-03-21T19:28:22.048+09:00  INFO 5564 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2023-03-21T19:28:22.076+09:00  INFO 5564 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-21T19:28:22.077+09:00  INFO 5564 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]
2023-03-21T19:28:22.185+09:00  INFO 5564 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-21T19:28:22.186+09:00  INFO 5564 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1511 ms
2023-03-21T19:28:22.791+09:00  INFO 5564 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''
2023-03-21T19:28:22.799+09:00  INFO 5564 --- [demo] [           main] jp.co.confrage.demo.DemoApplication      : Started DemoApplication in 2.996 seconds (process running for 
3.824)
aaa
bbb
ccc

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました