jar 文件中的媒体文件夹 [英] Media folder in jar file

查看:26
本文介绍了jar 文件中的媒体文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之前有人提过,我搜索了很多答案.它总是以你想要 getResourceAsStream"结尾.这不是我要找的.

我的问题是,对于游戏对象,我使用文件夹结构来保留精灵条,而不是拥有一个大精灵.这导致:

媒体/角色名字/动画名称/图片扩展

编程对象只是将它的文件夹保存为一个字符串,我将 getResource() URL 传递给一个对象以填充图像映射.可以有 {n} 个 AnimationName/子目录.我的错误来自此代码:

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

我获取目录,然后调用 listFiles 并将找到的文件名传递给 sprite 加载器.这是一个代码片段:

 dir = new File(s.toURI());文件[] chld = dir.listFiles();//获取图像/字符文件夹中的文件列表for(文件 f:chld){//获取每个目录的文件列表File[]grandChild = f.listFiles();for(文件t:grandChild){字符串 fname = t.getAbsolutePath();System.out.println(fname);字符串临时 = fname;temp = temp.substring(temp.lastIndexOf("/") + 1,temp.lastIndexOf("."));String animName = temp.replaceAll("\d*$", "");int numPics = 0;模式 p = Pattern.compile("[0-9]+");匹配器 m = p.matcher(temp);而(m.find()){numPics = Integer.parseInt(m.group());}System.out.println("动画名称:" + animName);System.out.println("文件名:" + fname);System.out.println("图片数量:" + numPics);Animations.put(animName, sl.loadStripImageArray(fname, numPics));}}

请原谅糟糕的命名和临时变量,它仍在处理中.

sl 是精灵加载器,Animations 是一个哈希映射.这工作正常,直到我打包项目.我不想编写一堆复杂的代码,这些代码仅在我有 jar 文件时才有效,而在我使用带有源文件夹的 netbeans 时则无效.

我已经考虑过有一个应用程序数据文件夹,但如果可以的话,我想保留一个 jar 包.

解决方案

仍然想使用 getResourceAsStream.这里没有任何要求所有资源必须保存在 JAR 内的同一文件夹中.您可以使用相对路径或绝对路径到 JAR 的根目录,方法是在路径前加上 / 前缀.

您不能让 File 使用 JAR 中的资源 - 即使使用指向包含在 JAR 中的资源的 URL 进行实例化也是如此.

您可能需要返工一些其他的事情,因为类路径并不是真正要枚举的(因为您当前正在列出父目录中的文件).它旨在按名称检索资源.所以一种可能性(我会推荐)是有一个清单"文件,其中包含要从每个目录加载的文件.(阅读此文件,然后按名称加载其他资源.)

或者,如果您能找到正在加载的 JAR 文件的名称,您可以创建一个 Jarfile ,然后调用它的 entries() 方法来查找所有包含的资源.但即便如此,它们也不会以树结构"的形式返回,因此理想情况下,您应该阅读这个,从中创建自己的树结构(可能是一系列 Map),然后使用它来检索目录"列表"根据需要.

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.

解决方案

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.

这篇关于jar 文件中的媒体文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆