java 我可以列出给定包中的资源吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/186496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:20:44  来源:igfitidea点击:

Can I list the resources in a given package?

javalistresourceswar

提问by Andreas Petersson

Lets assume my classes are loaded from a compressed .war file or loaded elsewhere, how can I discover all the resources in a given package? Enumerating files will not really work, since this is a war file. Most likely this will involve using the current classloader?

假设我的类是从压缩的 .war 文件加载或加载到其他地方,我怎样才能发现给定包中的所有资源?枚举文件不会真正起作用,因为这是一个War文件。这很可能涉及使用当前的类加载器?

Is there a library out there that does something like that? Googling revealed only some hacks with listing files.

有没有图书馆可以做这样的事情?谷歌搜索只发现了一些包含列表文件的黑客攻击。

采纳答案by SCdF

No, you can't list resources in a package because the JVM simply doesn't know what resources are in what package. See this question.

不,您不能列出包中的资源,因为 JVM 根本不知道哪个包中有哪些资源。看到这个问题

回答by jcfolsom

Yes. You can use ClassLoader.getResources("");

Or you could use ferret API:
https://www.ohloh.net/p/pureperfect-ferret

是的。你可以使用ClassLoader.getResources("");

或者你可以使用 ferret API:https:
//www.ohloh.net/p/pureperfect-ferret

This API allows you to scan the class path as well as arbitrary archives and directories using the Visitor pattern.

此 API 允许您使用访问者模式扫描类路径以及任意档案和目录。

回答by DerHeiligste

The Spring Framework offers a class called PathMatchingResourcePatternResolver. With this class you can give Ant style paths with wildcards and it will find the relevant resources for you (searching through the folders and jars on the classpath).

Spring 框架提供了一个名为PathMatchingResourcePatternResolver. 使用这个类,您可以使用通配符指定 Ant 样式路径,它会为您找到相关资源(搜索类路径上的文件夹和 jar)。

回答by Vlad Patryshev

I do this: - before packaging, ls myResources/*.ext > myResources/list.txt- then just read the file first

我这样做: - 在打包之前,ls myResources/*.ext > myResources/list.txt- 然后先读取文件