gpt4 book ai didi

java - 为什么我的 PathResource 不可读?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:59 31 4
gpt4 key购买 nike

我尝试编写一个读取 csv 文件的 Spring Batch 应用程序并将其内容存储在数据库中。我无法创建一个 FlatFileItemReader 因为我得到一个 IllegalStateException说明 Input 资源必须是可读的(阅读器处于“严格”模式)

这是我给读者的配置:

@Bean
public ItemReader<CadSystem> cadSystemReader(final Path backupDirectory,
final CadSystemFieldSetMapper fieldSetMapper) {
final FlatFileItemReader<CadSystem> reader = new FlatFileItemReader<>();
final DefaultLineMapper<CadSystem> lineMapper = new DefaultLineMapper<>();
final PathResource resource = new PathResource(backupDirectory.resolve("cad_systems.csv"));

lineMapper.setLineTokenizer(new DelimitedLineTokenizer(";"));
lineMapper.setFieldSetMapper(fieldSetMapper);
reader.setResource(resource);
reader.setLineMapper(lineMapper);

return reader;
}

这是堆栈跟踪:

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:386)
at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:210)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:227)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:121)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:115)
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at App.main(App.java:11)
Caused by: java.lang.IllegalStateException: Input resource must be readable (reader is in 'strict' mode): path [D:\backup\cad_systems.csv]
at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:259)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
... 20 common frames omitted

该文件位于 D:\backup\cad_systems.csv,所有用户/组均可读取。

是什么导致了这个错误?

更新

我试过像这样使用FileSystemResource

final FileSystemResource resource = new FileSystemResource(backupDirectory.resolve("cad_systems.csv").toFile());

现在可以了。但是 PathResource 有什么问题呢?

最佳答案

错误是由 bug in the JDK 7 引起的.使用 Java 8 时不会出现此问题。

关于java - 为什么我的 PathResource 不可读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29937125/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com