jar包中的資源讀取不到,真心難過,後來發現可以通過流的方式把資源讀取出來(包括.class本身)
//讀取jar包中的資源,但是讀到的是InputStream
InputStream is=this.getClass().getResourceAsStream("/Plugin/ComUI/Resources/blupin.png");
//接下來可以轉爲file,參照最下面的方法
//這裏我要處理圖片,所以直接就讀取出來
BufferedImage img = ImageIO.read(is);
這個是一個InputStream轉換爲file文件的方法。
public void inputstreamtofile(InputStream ins,File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}