Is there a good library for java that handles I/O of resources transparently when it comes to input sources from eg. java projects/folders or jar archives?
I would like to use the same code for reading resources in my application when I:
- Run the code from eclipse (from source)
- Run the deployed application (binaries)
- Get a list of resources from a folder (src/bin).
Normally I do:
URL resource = this.getClass().getClassLoader().getResource("someResource.ext");
which works fine both from source and from binaries assuming the resources are on the classpath. But in some of the projects only the subfolder is on the classpath and not its content (to avoid having to specify all the files as separate resources):
project
src
resources
someResource.txt
subFolder
-> resA.txt
-> resB.txt
-> resC.txt
....
In this case I need to do some low-level spagetti like this:
http://www.java-gaming.org/topics/how-to-check-if-a-url-is-a-directory/21435/view.html
I have looked a apache.commons-io but it does not quite what I need. Any suggestions for other libraries that handles nested folders/resources for both src and bin?
EDIT:
As recommended below I am trying to use an URL protocol handler as described here:
but I am not sure I understand it fully. In a project I have dependency on a jar file with a folder inside:
myjar.jar
myresources
> fileA.txt
> fileB.txt
> fileC.txt
In my application I do:
URL url = this.getClass().getClassLoader().getResource("myresources");
System.out.println(url.getPath());
Handler.install();
URLConnection con = new DataConnection(url);
try {
inputStream = con.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
Where DataConnection and Handler is copied from:
but I don't see how that helps me with fetching the resources inside the 'myresources' folder. I am clearly not using this in the right way.