使用.getResource()从.jar获取文件 [英] Getting File from .jar using .getResource()
本文介绍了使用.getResource()从.jar获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试访问.jar中的文件。在Netbeans中,这个:
I'm trying to acces a File inside the .jar. In Netbeans, this:
System.out.println(new File(this.getClass().getResource("Image.jpg").getFile()).exists());
打印出来:
true
然而,在构建之后,它会打印为false。
After building however, it prints false.
该文件肯定在.jar中,在.class文件旁边,如下所示:
The file is definitely in the .jar, next to the .class file, and something like this:
new Frame(){
@Override
public void paint(Graphics g){
try{
g.drawImage(ImageIO.read(this.getClass().getResource("Image.jpg")), 0, 0, this);
} catch(Exception e){e.printStackTrace();}
}
}.setVisible(true);
在构建后绘制图像。
如何将Image.jpg作为File对象访问?
How can I acces the Image.jpg as a File object?
推荐答案
文件已打包,请尝试
resource.getInputStream()
或
InputStream streamOfYourImg = this.getClass().getRessourceAsStream("com/package/resources/image.jpeg");
希望有所帮助
这篇关于使用.getResource()从.jar获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文