Input resource must exist (reader is in ‘strict‘ mode): class path resource

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader

使用spring batch 产生报错,很明显提示的是没有文件输入,可是我明明把文件放在了resource里了,这什么情况?

protected void doOpen() throws Exception {
        Assert.notNull(this.resource, "Input resource must be set");
        Assert.notNull(this.recordSeparatorPolicy, "RecordSeparatorPolicy must be set");
        this.noInput = true;
        if (!this.resource.exists()) {
            if (this.strict) {
                throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): " + this.resource);
            } else {
                logger.warn("Input resource does not exist " + this.resource.getDescription());
            }
        } else if (!this.resource.isReadable()) {
            if (this.strict) {
                throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode): " + this.resource);
            } else {
                logger.warn("Input resource is not readable " + this.resource.getDescription());
            }
        } else {
            this.reader = this.bufferedReaderFactory.create(this.resource, this.encoding);

            for(int i = 0; i < this.linesToSkip; ++i) {
                String line = this.readLine();
                if (this.skippedLinesCallback != null) {
                    this.skippedLinesCallback.handleLine(line);
                }
            }

            this.noInput = false;
        }
    }

if (!this.resource.exists()) {
            if (this.strict) {
                throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): " + this.resource);
            } else {
                logger.warn("Input resource does not exist " + this.resource.getDescription());
            }

可以发现报错首先因为resource不存在,然后是处于strict状态,但主要是因为没找到文件所以报错了。
完了又看看代码没什么问题,那就清清缓存,file->invalidate caches,成功了,原来是因为resource目录下的文件很容易出现缓存问题。

但是后面清了缓存又出问题了,因为之前是在resource目录下提取文件,后面要从某个盘中获取,但代码中我用了resource(new PathResource(txtPath)) 。这边我本来用了ClassPathResource,会不停爆错提示找不到文件,实际是因为ClassPathResource是用来获取resource目录下的文件的,要改成PathResource

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值