0
@Value("file:" + "${d:/my/dir/}")
private Resource dir;

How can I get the number of files in that resource dir? Unfortunately the Resource interfaces does not offer any getFiles() method...

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • possible duplicate of [Java resource as file](http://stackoverflow.com/questions/676097/java-resource-as-file) search through the site a bit before posting, the answer is likely already there. specifically [this answer](http://stackoverflow.com/a/1089408/1197030) – Joeblade Jan 20 '15 at 09:57
  • I know there are possibilities with native java file package. But I explicit requested the use of spring `Resource`, maybe there is also a chance of directly getting the dir content through the spring. – membersound Jan 20 '15 at 10:09
  • 1
    my appologies, I had missed the spring Resource part, but if it is a directory the Resource points to then you can simply use getFile() on it to get a File ref. and on File you can call isDirectory and list(). At least based on the Resource interface, this seems to be the case – Joeblade Jan 20 '15 at 16:53

1 Answers1

1

Based on this version of the Resource interface, you can call getFile

File theDir = dir.getFile();
if (theDir.isDirectory()) {
    theDir.list();
}

if you combine this with the propertyplaceholderconfigurer you might get what you're lookign for:

Community
  • 1
  • 1
Joeblade
  • 1,735
  • 14
  • 22