访问jar文件中的文件? [英] Access file in jar file?
问题描述
我需要能够访问存储在已编译 jar 文件中的文件.我已经弄清楚如何将文件添加到项目中,但是我将如何在代码中引用它?如何将文件从 jar 文件复制到用户硬盘驱动器上的某个位置?我知道有几十种方法可以访问文件(FileInputStream、FileReader 等),但我不知道如何查看其内部.
I need to be able to access a file stored in a compiled jar file. I have figured out how to add the file to the project, but how would I reference it in the code? How might I copy a file from the jar file to a location on the user's hard drive? I know there are dozens of ways to access a file (FileInputStream, FileReader, ect.), but I don't know how to look inside itself.
推荐答案
你可以这样使用:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileFromJarFile);
如果 foo.txt 在您的 JAR 文件的根目录中,您将使用:
If foo.txt was in the root of your JAR file, you'd use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("foo.txt");
假设该类与资源在同一个 JAR 文件中,我相信.
assumes the class is in the same JAR file as the resource, I believe.
这篇关于访问jar文件中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!