membersound membersound - 10 months ago 108
Java Question

How to read all files in a folder with spring-batch and MultiResourceItemReader?

I want to configure

spring-batch
to read all
csv
files inside a specific folder sequentially.

The following does not work because the delegate will try to open a file named
*.csv
, which of course is invalid. What do I have to change here?

@Bean
public ItemReader<String> reader() {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(new Resource[] {new FileSystemResource("/myfolder/*.csv")});
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}


The equivalent xml configuration would be written as follows, how could I rewrite it to java only config?

<bean id="reader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="/mypfolder/*.csv"/>
<property name="delegate" ref="flatFileItemReader"/>
</bean>

Answer Source

I think you should use a PathMatchingResourcePatternResolver.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download