java 如何列出jar包中jar包里的所有文件和目錄?
問題描述
如這樣一個目錄/Users/xxx/IdeaProjects/abc/web/target/web.jar!/BOOT-INF/lib/rest-0.0.1.jar!/com/tg/tiny/Users/xxx/IdeaProjects/abc/web/target/web.jar這個jar包下的文件目錄可以這樣得到
JarFile localJarFile = new JarFile(new File('/Users/xxx/IdeaProjects/abc/web/target/web.jar'));Enumeration<JarEntry> entries = localJarFile.entries();while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); System.out.println(jarEntry.getName());}
那么這個web.jar里的rest-0.0.1.jar下的文件目錄如何得到?
問題解答
回答1:URL url = new URL('jar', null, 0, 'file:/Users/xxx/IdeaProjects/web/target/web.jar!/BOOT-INF/lib/rest.jar');URLConnection con = url.openConnection();if (con instanceof JarURLConnection) { JarURLConnection result = (JarURLConnection) con; JarInputStream jarInputStream = new JarInputStream(result.getInputStream()); JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) {System.out.println(entry.getName()); }}回答2:
jar包是個文件。不是目錄。需要通過classloader的getResourceAsStream()。
package edu.hxraid; import java.io.*; public class Resource { public void getResource() throws IOException{ //返回讀取指定資源的輸入流 InputStream is=this.getClass().getResourceAsStream('/resource/res.txt'); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String s=''; while((s=br.readLine())!=null) System.out.println(s); } }
詳細使用:http://www.myexception.cn/pro...
相關文章:
1. sql語句 - 如何在mysql中批量添加用戶?2. shell - Update query wrong in MySQL3. javascript - mysql插入數據時怎樣避免與庫中的數據重復?4. php - 數據庫表如果是null怎么替換為其他字段的值5. 事務 - mysql共享鎖lock in share mode的實際使用場景6. 怎么php怎么通過數組顯示sql查詢結果呢,查詢結果有多條,如圖。7. mysql - JAVA怎么實現一個DAO同時實現查詢兩個實體類的結果集8. SQLAlchemy 訪問Mysql數據庫彈出Warning,VARIABLE_VALUE,如何解決?9. mysql - 數據庫建字段,默認值空和empty string有什么區別 11010. mysql建表報錯,查手冊看不懂,求解?
