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

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

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

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

      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天全站免登陆