Java .class.getResourceAsStream()

倾然丶 夕夏残阳落幕 提交于 2019-12-23 09:48:48

问题


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.

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

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.


回答1:


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



回答2:


this.getClass().getResourceAsStream("myImage.png");

Make sure your image is there in the classpath. Need not put it into any JAR as such.



来源:https://stackoverflow.com/questions/14977851/java-class-getresourceasstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!