Java 如何从JAR存档中读取文件?
我需要从Java 如何从JAR存档中读取文件?,java,jar,Java,Jar,我需要从test.jar文件中读取/test/a.xml的内容(当然,它们都是变量,不是常量)。最简单的方法是什么 File file = new File("test.jar"); String path = "/test/a.xml"; String content = // ... how? 使用ZipInputStream搜索您请求的文件 FileInputStream fis = new FileInputStream(args[0]); ZipInputStream zis =
test.jar
文件中读取/test/a.xml
的内容(当然,它们都是变量,不是常量)。最简单的方法是什么
File file = new File("test.jar");
String path = "/test/a.xml";
String content = // ... how?
使用
ZipInputStream
搜索您请求的文件
FileInputStream fis = new FileInputStream(args[0]);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null)
if(ze.getName().equals("/test/a.xml")
{
//use zis to read the file's content
}
这个怎么样:
JarFile file = new JarFile(new File("test.jar"));
JarEntry entry = file.getJarEntry("/test/a.xml");
String content = IOUtils.toString(file.getInputStream(entry));
您的应用程序也从test.jar运行吗?不,我的应用程序不是从test.jar运行的。您使用的是Java 7吗?您可以尝试改编以下示例:如何使用zis读取文件内容?@mendel如果您可以像常规的
InputStream
一样使用fis
,它将为您提供文件的内容