Java .class.getResourceAsStream() [英] Java .class.getResourceAsStream()

查看:264
本文介绍了Java .class.getResourceAsStream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, Class.getResourceAsStream()获取一个 InputStream 指向一个文件.jar文件?如果我对此有误,请纠正我。

To the best of my knowledge, Class.getResourceAsStream() gets an InputStream directed at a file within the .jar file? If I'm wrong about this, please correct me.

有没有办法暂时将外部文件加载到.jar中,以便 getResourceAsStream()方法可以检索它吗?

Is there any way to temporarily 'load' an external file into the .jar, so that the getResourceAsStream() method can retrieve it?

例如加载 C:/folder/file.txt 进入.jar,以便 MyClass.class
.getResourceAsStream(file.txt)
将返回一个有效的 InputStream 到该文件,或至少已经暂时加载到.jar的副本。

For example loading C:/folder/file.txt into the .jar so that MyClass.class .getResourceAsStream("file.txt") would return a working InputStream to the file, or at least a copy of it that has been 'loaded' temporarily into the .jar.

我'对不起,我的措辞非常糟糕,我希望你能理解我正在做的事情,不太确定如何解释它。

I'm sorry I've worded this so badly, I hope you can understand what I'm trying to do, wasn't quite sure how to explain it simply.

推荐答案

不,getResourceAsStream()方法从类路径中获取文件。这包括在命令行上指定的任何内容(Windows版本,linux使用':'作为分隔符):

Nope, the getResourceAsStream() method gets a file from the classpath. That includes anything that is specified like this on the command line (windows version, linux uses ':' as delimiter):

java -cp "path;path2;path3;path4/some.jar" your.main.Clazz

这篇关于Java .class.getResourceAsStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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