如何从jar中读取尚未加载到内存中的属性文件



我想动态加载一个jar文件,但是在加载之前,我想在其根目录中读取属性文件。是否有一种方法来读取属性文件,从jar与外部加载jar?

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

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

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

这就是我的解决方案

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;
}

最新更新