2

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:

  1. Run the code from eclipse (from source)
  2. Run the deployed application (binaries)
  3. 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:

JEditorPane with inline image

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:

JEditorPane with inline image

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.

Community
  • 1
  • 1
u123
  • 15,603
  • 58
  • 186
  • 303
  • I do not really get the problem. In general a given resource is in the classpath or not... In case of classpath you need a full qualified name (package + resource), in all other cases you can go with external path references or URL classloaders. – home Dec 12 '12 at 20:02
  • The problem can be boiled down to how to check if an url points to a directory or a file. I have only been able to find this: http://www.java-gaming.org/topics/how-to-check-if-a-url-is-a-directory/21435/view.html – u123 Dec 12 '12 at 21:01

1 Answers1

0

There is in java 7 the zip file system and other generalisations of file systems.

Of course you can also create URL protocol handlers.

Community
  • 1
  • 1
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I am having problems with the URL protocol approach, see my edited post, maybe you have some input? – u123 Dec 14 '12 at 13:19