Springboot环境下静态资源文件读取;ClassPathResource;FileNotFoundException

String strpath = “keyword.txt”; //获取文本文档路径Resource resource = new ClassPathResource(strpath);File file = resource.getFile();System.out.println(file.exists());InputStreamReader isr = new InputStreamReader(new FileInputStream(file), “gbk”);BufferedRea
摘要由CSDN通过智能技术生成

在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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值