How to load all files of a folder to a list of Resources in Spring?
If you are using Spring
@Autowired
private ApplicationContext applicationContext;
public void loadResources() {
try {
Resource[] resources = applicationContext.getResources("file:C:/XYZ/*_vru_*");
} catch (IOException ex) {
ex.printStackTrace();
}
}
Use ResourceLoader and ResourcePatternUtils:
class Foobar {
private final ResourceLoader resourceLoader;
public Foobar(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
Resource[] loadResources(String pattern) throws IOException {
return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);
}
}
and use it like:
Resource[] resources = foobar.loadResources("classpath*:../../dir/*.txt");