如何从不同的JAR中读取多个具有相同名称的资源文件?

时间:2021-02-04 16:37:03

If there are two JAR files in the classpath, both containing a resource named "config.properties" in its root. Is there a way to retrieve both files similar to getClass().getResourceAsStream()? The order is not relevant.

如果类路径中有两个JAR文件,则两个JAR文件的根目录中都包含名为“config.properties”的资源。有没有办法检索两个类似于getClass()的文件.getResourceAsStream()?订单无关紧要。

An alternative would be to load every property file in the class path that match certain criterias, if this is possible at all.

另一种方法是在类路径中加载匹配某些标准的每个属性文件,如果可能的话。

2 个解决方案

#1


26  

You need ClassLoader.getResources(name)
(or the static version ClassLoader.getSystemResources(name)).

您需要ClassLoader.getResources(name)(或静态版本ClassLoader.getSystemResources(name))。

But unfortunately there's a known issue with resources that are not inside a "directory". E.g. foo/bar.txt is fine, but bar.txt can be a problem. This is described well in the Spring Reference, although it is by no means a Spring-specific problem.

但不幸的是,有一个已知问题,资源不在“目录”中。例如。 foo / bar.txt很好,但bar.txt可能有问题。这在Spring Reference中有很好的描述,尽管它绝不是Spring特有的问题。

Update:

Here's a helper method that returns a list of InputStreams:

这是一个返回InputStream列表的辅助方法:

public static List<InputStream> loadResources(
        final String name, final ClassLoader classLoader) throws IOException {
    final List<InputStream> list = new ArrayList<InputStream>();
    final Enumeration<URL> systemResources = 
            (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
            .getResources(name);
    while (systemResources.hasMoreElements()) {
        list.add(systemResources.nextElement().openStream());
    }
    return list;
}

Usage:

List<InputStream> resources = loadResources("config.properties", classLoader);
// or:
List<InputStream> resources = loadResources("config.properties", null);

#2


0  

jar files are zip files.

jar文件是zip文件。

Open the file using java.util.zip.ZipFile

使用java.util.zip.ZipFile打开该文件

Then enumerate its entries looking for the properties file you want.

然后枚举其条目,查找所需的属性文件。

When you have the entry you can get its stream with .getInputStream()

当您有条目时,您可以使用.getInputStream()获取其流

#1


26  

You need ClassLoader.getResources(name)
(or the static version ClassLoader.getSystemResources(name)).

您需要ClassLoader.getResources(name)(或静态版本ClassLoader.getSystemResources(name))。

But unfortunately there's a known issue with resources that are not inside a "directory". E.g. foo/bar.txt is fine, but bar.txt can be a problem. This is described well in the Spring Reference, although it is by no means a Spring-specific problem.

但不幸的是,有一个已知问题,资源不在“目录”中。例如。 foo / bar.txt很好,但bar.txt可能有问题。这在Spring Reference中有很好的描述,尽管它绝不是Spring特有的问题。

Update:

Here's a helper method that returns a list of InputStreams:

这是一个返回InputStream列表的辅助方法:

public static List<InputStream> loadResources(
        final String name, final ClassLoader classLoader) throws IOException {
    final List<InputStream> list = new ArrayList<InputStream>();
    final Enumeration<URL> systemResources = 
            (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
            .getResources(name);
    while (systemResources.hasMoreElements()) {
        list.add(systemResources.nextElement().openStream());
    }
    return list;
}

Usage:

List<InputStream> resources = loadResources("config.properties", classLoader);
// or:
List<InputStream> resources = loadResources("config.properties", null);

#2


0  

jar files are zip files.

jar文件是zip文件。

Open the file using java.util.zip.ZipFile

使用java.util.zip.ZipFile打开该文件

Then enumerate its entries looking for the properties file you want.

然后枚举其条目,查找所需的属性文件。

When you have the entry you can get its stream with .getInputStream()

当您有条目时,您可以使用.getInputStream()获取其流