Search code examples
javaclasspathjava-7nio

Is there an equivalent to Files.newDirectoryStream for resources on the classpath


I have a project that uses this code to get a filtered list of files in a directory:

Path directoryPath = 
    FileSystems.getDefault().getPath("src/main/resources/com/foo/bar");
DirectoryStream<Path> ds = 
    Files.newDirectoryStream(directoryPath, filter);

This works great when I run the project in eclipse, but when we package up the jar, of course, it can't find the resources using this path. I could manually reference these resources if I knew their names ahead of time, but I'm hitting a wall trying to easily filter them, or even get a list of them. Is there a clean way to do this that references these files as resources instead of a specific directory location?


Solution

  • Have a look at Class.getResource or Class.getResourceAsStream and see if that works for you.