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. redis與mysql一致性問題2. macos - mac下docker如何設置代理3. 想練支付寶對接和微信支付對接開發(Java),好像個人不可以,怎么弄個企業的4. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?5. css - 求推薦適用于vue2的框架 像bootstrap這種類型的6. android - coordinatorLayout嵌套recyclerview7. mysql關聯更新不成功8. javascript - 微信支付:H5調起支付API,直接說支付失敗9. javascript - Web微信聊天輸入框解決方案10. javascript - [多圖預警]reactjs點擊某表格編輯內容,跳轉傳值this.context.router.params.id時id報錯未定義
