在jar文件中访问文件? [英] Access file in jar file?

查看:122
本文介绍了在jar文件中访问文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够访问存储在已编译的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆