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