从 jar 中读取文本文件 [英] Reading a text file from a jar
问题描述
我无法从 jar 中读取文本文件.我在解压缩时管理了它,但现在我已经将它存档,它不起作用.罐子里有:
I'm having trouble reading a text file from inside a jar. I managed it while it was unzipped but now that I've archived it it won't work. The jar has:
- BTA.jar
- Splash.class
- *.class
- Splash.txt
- 等等.
现在,我正在使用 BufferedReader 和 FileReader
Now, at the moment I'm using a BufferedReader and a FileReader
f = new FileReader("Splash.txt"); in = new BufferedReader(f);
但通过谷歌搜索我知道它只能从 jar 外部读取文件.
but from googling around I know that that only reads files from outside of the jar.
我的问题是:如何从 jar 中读取文件?
My question is: How do I read a file from inside the jar?
推荐答案
假设你正在执行
BTA.jar
,你可以使用Assuming you're executing the
BTA.jar
, you can useInputStream in = YourClass.class.getResourceAsStream("/Splash.txt");
检索
InputStream
并通过InputStreamReader
将其传递给BufferedReader
.to retrieve an
InputStream
and pass it to theBufferedReader
through anInputStreamReader
.定义了传递给方法的路径格式的规则此处,在
Class
类的 javadoc 中.The rules for the format of the path to pass to the method are defined here, in the javadoc for the
Class
class.这篇关于从 jar 中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!