从 jar 中读取文本文件 [英] Reading a text file from a jar

查看:25
本文介绍了从 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 use

    InputStream in = YourClass.class.getResourceAsStream("/Splash.txt");
    

    检索InputStream并通过InputStreamReader将其传递给BufferedReader.

    to retrieve an InputStream and pass it to the BufferedReader through an InputStreamReader.

    定义了传递给方法的路径格式的规则此处,在 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屋!

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