在Springboot环境下可以使用ClassPathResource类读取静态资源文件;ClassPathResource类的注释如下:
Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.
Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.
翻译过来就是:
类路径资源的资源实现。使用给定的ClassLoader或给定的类来加载资源。
如果类路径资源驻留在文件系统中,则支持解析为 java.io.File,如果是JAR中的资源则不支持。始终支持解析为URL。
举例如下:
静态资源文件放在 src/main/resources 目录下;
String strpath = "keyword.txt"; //获取文本文档路径
Resource resource = new ClassPathResource(strpath);
File file = resource.getFile();
InputStreamReader isr = new