SpringBoot打包jar文件读取resources目录下的文件

72 阅读1分钟

SpringBoot项目代码打包成jar文件,代码中如果需要读取resources目录下的文件,如下截图所示,例如:如果需要读取resources目录下test/test.txt文件。

1.png

java代码中可以采用类似下面的方式读取:

import org.springframework.core.io.ClassPathResource;

  ClassPathResource classPathResource = new ClassPathResource( " test / test.txt " );
String str = IOUtils.toString(classPathResource.getInputStream(),
"UTF-8" );