java 如何列出jar包中jar包里的所有文件和目錄?
問題描述
如這樣一個(gè)目錄/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這個(gè)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());}
那么這個(gè)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包是個(gè)文件。不是目錄。需要通過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); } }
詳細(xì)使用:http://www.myexception.cn/pro...
相關(guān)文章:
1. windows誤人子弟啊2. 冒昧問一下,我這php代碼哪里出錯(cuò)了???3. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)4. python - linux怎么在每天的凌晨2點(diǎn)執(zhí)行一次這個(gè)log.py文件5. 數(shù)據(jù)庫 - Mysql的存儲(chǔ)過程真的是個(gè)坑!求助下面的存儲(chǔ)過程哪里錯(cuò)啦,實(shí)在是找不到哪里的問題了。6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. mysql優(yōu)化 - MySQL如何為配置表建立索引?8. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問題
