4

This question is in reference with another stackoverflow post - How to load all files of a folder to a list of Resources in Spring?

I want to load all files from two specific folders using ResourceLoader. I am trying to use ResourcePatternUtils.

class Foobar { private ResourceLoader resourceLoader;

@Autowired
public Foobar(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
}

Resource[] loadResources(String pattern) throws IOException {
    return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);
}

}

Resource[] resources1 = foobar.loadResources("classpath*:../../folder1/*.txt");
Resource[] resources2 = foobar.loadResources("classpath*:../../folder2/*.txt");

But I need both the resources in a single array. Should I be using something like Java8 stream to concatenate them ?

Community
  • 1
  • 1
chrisrhyno2003
  • 3,906
  • 8
  • 53
  • 102

1 Answers1

4

This could be possible with org.springframework.core.io.support.ResourcePatternResolver#getResources interface where you can pass path as parameter.

resourcePatternResolver.getResources("classpath:folder/*.xml");

Implementation is on ApplicationContext so you can access from this interface also

Nagaraddi
  • 269
  • 2
  • 7
  • let's say i need to add resources from different folders, what syntax works in that case? `classpath:folder1/*.xml:folder2/*.xml` ? – Mohd Asim Jul 14 '18 at 01:02