1

I would love to know how I can read the whole entries from a package in Java. With getRessource() you can get a single file. But I want them all! My plan is to read all Files from a folder/package (without subdir) and then use a Stream to filter the entries.

Hope you can help me out.

zesaro
  • 33
  • 1
  • 7
  • You read them from a jar? Which is technically a zip file, so you can treat it as thus. – Uwe Allner Feb 11 '16 at 12:14
  • 3
    Possible duplicate of [How do I read all classes from a Java package in the classpath?](http://stackoverflow.com/questions/1456930/how-do-i-read-all-classes-from-a-java-package-in-the-classpath) – Ferrybig Feb 11 '16 at 12:34
  • 1
    well it is not so much the duplicates, since this topic is regarding the files(resources), however, the other one tackles the classes. – Ducaz035 Feb 11 '16 at 12:36

3 Answers3

1

You can achieve this using the Spring Framework. Even if you are not developing a Spring application, you can still use their utility methods. You want to use PathMatchingResourcePatternResolver:

public Resource[] getResources(String package)
{
    PathMatchingResourcePatternResolver pmrpr = new PathMatchingResourcePatternResolver();
    // turns com.myapp.mypackage into /com/myapp/mypackage/*
    return pmrpr.getResources("/" + package.replace(".", "/") + "/*");
}

For reference, see:

If you are using Maven, use this dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>
randers
  • 5,031
  • 5
  • 37
  • 64
1

You can do so with the Reflections library.

One may just create a reflections object and let it do the scanning for you. It would be as easy as doing something like

Reflections reflections = new Reflections("my.package");
Set<String> resourceStrings = reflections.getResources(s -> true /*match all resources*/);
Set<URL> resources = resourceStrings.stream()
                                    .map(s -> getClass().getResource(s))
                                    .collect(Collectors.toSet())
randers
  • 5,031
  • 5
  • 37
  • 64
Ducaz035
  • 3,054
  • 2
  • 25
  • 45
-1

You can get a list of all the files and then filter them:

File d = new File(System.getProperty("user.dir")); // user.dir returns the current working directory.

for(File f : d.listFiles())
{
    //do what you want to do
}
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • This looks nice. Now user.dir gave me all files in my root folder of the project. How can I change the parameter so I get the files in a specific directory like src/test/ressources ?? If I change the parameter like this I get a NullPointerException. – zesaro Feb 11 '16 at 12:25
  • @SaschaZeller Concatenate it. Use : `new File(System.getProperty("user.dir")+"/src/test/ressources")` – dryairship Feb 11 '16 at 12:36
  • 1
    Oh wow you guys... This answer is full of bad practice. What if your program is packaged as a jar? What if it is launched from outside of its build directory? And why are we concatenating file paths? This completely goes against the spirit of Java, which is write once - run everywhere. – randers Feb 13 '16 at 11:00