在Java小程序中加载目录中的所有文件
如何以编程方式加载小程序的 JAR 文件中给定目录中的所有资源文件?在程序的生命周期中,资源可能会更改几次,因此我不想对名称进行硬编码。
通常我只会使用 File.list() 遍历目录结构,但在尝试在其中执行此操作时会遇到权限问题一个小程序。我还研究了使用 ClassLoader.getResources() 行的枚举,但它只在 JAR 文件中查找同名的文件。
本质上我想做的是(类似的):
ClassLoader imagesURL = this.getClass().getClassLoader();
MediaTracker tracker = new MediaTracker(this);
Enumeration<URL> images = imagesURL.getResources("resources/images/image*.gif");
while (images.hasMoreElements()){
tracker.add(getImage(images.nextElement(), i);
i++;
}
我知道我可能缺少一些明显的功能,但我花了几个小时搜索教程和文档,寻找在未签名的小程序中执行此操作的简单方法。
How would one go about programatically loading all the resource files in a given directory in a JAR file for an applet? The resources will probably change several times over the lifetime of the program so I don't want to hardcode names in.
Normally I would just traverse the directory structure using File.list(), but I get permission issues when trying to do that within an applet. I also looked at using an enumeration with something line ClassLoader.getResources() but it only finds files of the same name within the JAR file.
Essentially what I want to do is (something like) this:
ClassLoader imagesURL = this.getClass().getClassLoader();
MediaTracker tracker = new MediaTracker(this);
Enumeration<URL> images = imagesURL.getResources("resources/images/image*.gif");
while (images.hasMoreElements()){
tracker.add(getImage(images.nextElement(), i);
i++;
}
I know I'm probably missing some obvious function, but I've spent hours searching through tutorials and documentation for a simple way to do this within an unsigned applet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过两种方式完成此操作:
image_1
、image_2
、image_3
),然后你就可以一次性收集你需要的所有资源。否则你需要编写大量代码。这个想法是你必须:
确定 JAR 文件的路径:
将其打开为
java.util.jar.JarFile
迭代所有条目并将其名称与给定掩码进行匹配
对于其他代码支持,我将向您推荐 Spring PathMatchingResourcePatternResolver#doFindPathMatchingJarResources()。
You can do it in two ways:
image_1
,image_2
,image_3
), then you can collect all resources you need in one go.Otherwise you need to code a lot. The idea is that you have to:
determine the path to your JAR file:
open it as
java.util.jar.JarFile
iterate through all entries and match their name with a given mask
For additional code support, I will refer you to Spring PathMatchingResourcePatternResolver#doFindPathMatchingJarResources().