[英]Read files from a folder inside a jar file on classpath
我有一個帶有圖形編輯器的項目(例如GMF技術),我需要讀取放置在項目類路徑上的.Jar庫中的文件。
VectorSync_1.0.0.201403121100.jar的結構類似於:
VectorSync:
我導入.Jar文件的項目的.classpath是:
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/home/jperezmedina/git/arom/arom-core/target/scala-2.9.0/aromslave_2.9.0-0.1.jar"/>
<classpathentry kind="lib" path="/home/jperezmedina/runtime-EclipseApplication/DefaultProject.arom/lib/VectorSync_1.0.0.201403121100.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
我使用此方法嘗試訪問.xml文件:
JarFile file = new JarFile("VectorSync_1.0.0.201403121100.jar");
BufferedReader input1 =
new BufferedReader(new InputStreamReader(file.getClass().
getClassLoader().getResourceAsStream("model/VectorSync.xml")));
但是使用的方法始終返回null
。
您能幫我解決這個問題嗎?
謝謝
您嘗試過這種方法嗎?
Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
如果查看JarFile
您會發現它為您提供了執行此操作的api
JarFile file = new JarFile("VectorSync_1.0.0.201403121100.jar");
ZipEntry entry = file.getEntry("model/VectorSync.xml");
BufferedReader input1 = new BufferedReader(
new InputStreamReader(file.getInputStream(entry)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.