关于“classpath“找不到系统文件FileNotFoundException;

在学习poi时经常要使用到获取模板,也经常会出现系统找不到文件的错误;

public static void main(String[] args) throws Exception {
    //我们先获取模板文件;
    File rootPath = new File(ResourceUtils.getURL("classpath:").getPath());
    log.info(rootPath.toString());
    File template = new File(rootPath, "/static/word_template/test.docx");
    log.info(template.toString());
    FileInputStream inputStream = new FileInputStream(template);
    XWPFDocument document = new XWPFDocument(inputStream);

}

D:\develop\IdeaProjects\poi_test\target\classes\static\word_template\test.docx

当上述代码在java/主代码目录下它找的是编译后的classes包下的模板文件,我们平常将模板放在resouce包下,这样是可以找的到的,但是当我们使用测试代码时,由于测试代码是在test包下,他寻找的是编译后的test_classes这个包,这个包下面是没有模板文件的,就会发生错误,此外在出现该错误时即使在java包下的正常代码也会出现,可能时在加入编译时没有正常编译导致的,此时删除target包进行重新编译即可;

public void testA() throws Exception {
        //我们先获取模板文件;
        File rootPath = new File(ResourceUtils.getURL("classpath:").getPath());
        log.info(rootPath.toString());
        File template = new File(rootPath, "/static/word_template/test.docx");
        log.info(template.toString());
        FileInputStream inputStream = new FileInputStream(template);
        XWPFDocument document = new XWPFDocument(inputStream);
java.io.FileNotFoundException: D:\develop\IdeaProjects\poi_test\target\test-classes\static\word_template\test.docx (系统找不到指定的路径。)

	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at com.heima.PoiTestApplicationTests.testA(PoiTestApplicationTests.java:25)
	at su
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值