0

我想动态加载一个 jar 文件,但是在加载它之前我想在它的根目录中读取一个属性文件。有没有办法在不加载 jar 的情况下从 jar 中读取属性文件?

我试过这个,但后来意识到这是我在加载 jar 后要做的事情。

    InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream("jdscfg.properties");

我是否需要考虑将文件视为 zip 并提取文件?

4

1 回答 1

0

这是我的解决方案

public static InputStream getInputStreamFromZip(File f, String fileEntry) {
    try {
        ZipFile zf = new ZipFile(f);
        Enumeration entries = zf.entries();
        while (entries.hasMoreElements()) {
            ZipEntry ze = (ZipEntry) entries.nextElement();
            System.out.println("Read " + ze.getName() + "?");
            if (ze.getName().equalsIgnoreCase(fileEntry)) {
                return zf.getInputStream(ze);
            }
        }
    } catch (IOException e) {
        System.err.println("io error accessing zip file");
    }
    return null;
}
于 2013-02-27T20:20:45.937 回答