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

membersound picture membersound · Jul 29, 2015 · Viewed 20.6k times · Source

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

Tharindu Jayasuriya picture Tharindu Jayasuriya · Aug 17, 2017

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;

}