问题
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