How to read all files in a folder with spring-batch and MultiResourceItemReader?
Alternatively @Value annotation can be used to let ResourceArrayPropertyEditor convert string to Resource[].
@Bean
@StepScope
public ItemReader<String> reader(
@Value("file:/myfolder/*.csv") Resource[] resources
) {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
I think you should use a PathMatchingResourcePatternResolver
.
Read multiple files from Absolute file path:
@Bean
public MultiResourceItemReader<POJO> multiResourceItemReader() {
MultiResourceItemReader<POJO> resourceItemReader = new MultiResourceItemReader<POJO>();
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("file:" + filePath );
resourceItemReader.setResources(resources);
resourceItemReader.setDelegate(reader());
return resourceItemReader;
}
@Bean
public FlatFileItemReader<POJO> reader() {
FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();
reader.setLineMapper(new DefaultLineMapper() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id1", "POJO"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
setTargetType(POJO.class);
}});
}});
return reader;
}
Use PathMatchingResourcePatternResolver like this.
@Bean
public ItemReader<String> reader() {
Resource[] resources = null;
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
resources = patternResolver.getResources("/myfolder/*.csv");
} catch (IOException e) {
e.printStackTrace();
}
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}