从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
- * .classory
- Splash.txt
- 等..
- BTA.jar
- Splash.class
- *.class
- Splash.txt
- etc..
现在,我正在使用BufferedReader和FileReader
Now, at the moment I'm using a BufferedReader and a FileReader
f = new FileReader("Splash.txt"); in = new BufferedReader(f);
但是从google搜索我知道只能从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
并将其传递给BufferedReader
通过InputStreamReader
。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屋!