2013-02-26 64 views
0

我想在加载之前动态地加载一个jar文件我想在其根目录中读取一个属性文件。有没有一种方法可以从jar中读取属性文件,而无需加载jar?如何从尚未加载到内存的jar中读取属性文件?

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

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

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

回答

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