如何从jar获取文件? [英] How to get a file from jar?

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

问题描述

我需要一个文件 filename.txt ,它位于一个jar文件中。如何使用代码读取txt文件的内容?是否有可用于处理* .jar文件的库?

I need a file filename.txt which is inside a jar file. How can I read the contents of the txt file using code? Are there any libraries available for working with *.jar files?

推荐答案

public class Test {
    public static void main(String[] args) {
        InputStream stream = Test.class.getResourceAsStream("/test.txt");
        Scanner s = new Scanner(stream);
        while (s.hasNext()) {
            System.out.println(s.next());
        }
    }
}

jar需要打开应用程序的类路径。

The jar needs to be on the classpath of the application.

此代码将在所有jar的根目录中搜索文件test.txt。

This code will search for the file test.txt in the root of all jars.

如果你需要指定一个不在类路径上的jar,那么其他建议可能更值得。

If you need to specify a jar that isn't on the classpath, then the other suggestions might be more worthwhile.

这篇关于如何从jar获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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