gpt4 book ai didi

java - 从 jar 文件中的文件夹加载所有文件

转载 作者:行者123 更新时间:2023-12-02 07:36:42 25 4
gpt4 key购买 nike

如何加载 jar 文件中文件夹中的所有文件?

到目前为止,我一直从这样的文件夹加载文件 File[] files = new File("res/Models/"+ dir).listFiles(); 但它不在一个 fat jar 子中文件(我得到一个 NullPointerException)。

注意:应该加载文件的类和我想要加载的文件位于同一个 jar 文件中。当我在 Eclipse 中运行程序时,我在那里写的行有效,但是当我导出 jar 文件时,我得到该行的 NullPointerException

最佳答案

jar 文件是一个 zip 文件,因此您可以使用 ZipInputStream 读取其内容,如下所示:

  ZipFile zipFile = new ZipFile(file);
ZipInputStream stream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
ZipEntry entry = null;
List<String> filePaths = new ArrayList<String>();
while ((entry = stream.getNextEntry()) != null) {
// this will get you the contents of the file
InputStream inputStream = zipFile.getInputStream(entry);
// this add the file path to the list
filePaths.add(entry.getName());
}

您还可以执行以下操作

关于java - 从 jar 文件中的文件夹加载所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com