Why is my PathResource not being read?
I am trying to write a Spring Batch Application that reads a csv file and stores its contents in a database. I cannot create a FlatFileItemReader
because I get IllegalStateException
that the input resource must be readable (the reader is in "strict" mode).
This is my configuration for the reader:
@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;
}
And this is the stacktrace:
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
The file exists in D:\backup\cad_systems.csv
and is available to all users / groups.
What is causing this error?
Update
I tried to use FileSystemResource
like this
final FileSystemResource resource = new FileSystemResource(backupDirectory.resolve("cad_systems.csv").toFile());
and now it works. But what's wrong with PathResource
?
source to share