3

This question has been brought up before, and I have searched many of the answers. It always ends in "You want getResourceAsStream". This is not what I am looking for.

My issue is , for a game object, I am using a folder structure to keep sprite strips rather than having one large sprite. This results in :

Media/ CharacterName/ AnimationName/ image.extension

the programming object just holds it's folder as a string, and I pass the getResource() URL to an object to fill the map of images. there can be {n} number of AnimationName/ sub directories. My error comes from this code:

dir = new File(s.toURI());

I take the directory, and call listFiles and pass the file names found to the sprite loader. Here is a code snippet:

    dir = new File(s.toURI());
    File[] chld = dir.listFiles();
    //get a list of files in the image/character folder
    for(File f:chld)
    {
        //get a list of the files for each dir
        File[] grandChild = f.listFiles();
        for(File t:grandChild)
        {
             String fname = t.getAbsolutePath();
             System.out.println(fname);
             String temp = fname;
             temp = temp.substring(temp.lastIndexOf("/") + 1,temp.lastIndexOf("."));
             String animName = temp.replaceAll("\\d*$", "");
             int numPics = 0;
             Pattern p = Pattern.compile("[0-9]+");
             Matcher m = p.matcher(temp);
             while(m.find()){
                 numPics = Integer.parseInt(m.group());
             }
             System.out.println("animation name: " + animName);
             System.out.println("file name: " + fname);
             System.out.println("number of pictures: " + numPics);
            Animations.put(animName, sl.loadStripImageArray(fname, numPics));
        }
    }

Excuse the poor naming and temp variables, it's still being worked on.

sl is the sprite loader, and Animations is a hash map. This works fine until I package the project. I don't want to write a bunch of convoluted code that only works if I have a jar file, and not when I'm working in netbeans with the source folders.

I have considered having an application data folder, but I'd like to stay with a jar package if I can.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
loctrice
  • 2,454
  • 1
  • 23
  • 34

2 Answers2

1

You do still want to use getResourceAsStream. Nothing here requires that all resources must be kept at the same folder within the JAR. You can use relative paths, or absolute paths to the root of the JAR by prefixing your path with /.

You can't make File work with resources within the JAR - even if instantiated with a URL that points to a resource contained within a JAR.

You may have to rework some other things, as the classpath is not really meant to be enumerated against (as you're currently listing files from the parent directory). It is designed to retrieve a resource by name. So one possibility (that I would recommend) is to have a "manifest" file that contains the files you want to load from each directory. (Read this file, then load the additional resources by name.)

Alternatively, if you can find the name of the JAR file you're loading from, you can create a Jarfile from it, then call its entries() method to find all of the contained resources. But even then, they aren't returned in a "tree structure", so ideally, you'd read this one, create your own tree structure from it (possibly as a series of Maps), then use it to retrieve the "directory listings" as needed.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
0

If you are absolutely sure that the sprites are located in a jar - you could try using the JarFile class. There is a method entries. I didn't try it but it seems to return all resources located in the whole jar file. You would have to filter out which resources are in the right path.

Marc-Christian Schulze
  • 3,154
  • 3
  • 35
  • 45
  • Thanks for the answer. However, this only works if I have a jar package in my netbeans project. I don't want to have my media in a jar until it's packaged. I am going to be reading on the JarFile class though ;) – loctrice Dec 30 '11 at 17:26