项目结构:

project-name
    src/main/java
    src/main/resource
        data
            input.txt    

输入文件目录在src/main/resource目录下的data/input.txt 文件

 

第 1 种方式:(推荐)

在class类里获取Resource对象:

@Value("classpath:data/input.txt")
private Resource inputResource;

第 2 种方式:

ClassPathResource resource = new ClassPathResource("data/input.txt", this.getClass().getClassLoader());

注意:这里一定要设置classLoader参数,否则无效,原因不明。

第 3 种方式:

ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource inputFile = resourceLoader.getResource("data/input.txt");

end.

相关文章:

  • 2022-12-23
  • 1970-01-01
  • 2022-01-01
  • 2021-11-27
  • 2021-08-15
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
相关资源
相似解决方案