2014-11-14 113 views
1

基本上我想要做的是解析通過一個目錄的所有文件,並將所有的孩子添加到列表< File>。 問題在於我沒有主要功能,我只是運行一個jar。所以我不能這樣做:如何從jar中獲取資源(文件夾)?

URL url = MyClass.class.getResource("resources/"); 
if (url == null) { 
    // error - missing folder 
} else { 
    File dir = new File(url.toURI()); //throws error when running the jar 
    for (File nextFile : dir.listFiles()) { 
     // Do something with nextFile 
    } 
} 

因爲它拋出IllegalArgument異常。我也嘗試過使用InputStream:

InputStream inStream = CompilerUtils.class.getClassLoader().getResourceAsStream(filePath) 

但是它似乎不適用於文件。我正在尋找這個可用於目錄的替代方案。網址url.getFile()

回答

0

獲取字符串represenataion它看起來像它file:/C:/xxx/yyy/x.jar!/resources解壓路徑:C:/xxx/yyy/x.jar.使用jar文件類讀取文件夾

相關問題