58,454
社区成员
发帖
与我相关
我的任务
分享
File f = new File("Chock.png1");
System.out.println(f.getAbsolutePath());
System.out.println(f.exists());
打印出来的结果与实际的物理路径是有差别的,所以是不靠谱的,可以用下面这种方式,虽然走了点弯路
try {
InputStream is = Main.class.getResourceAsStream("Chock.png");
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[128];
int length = 0;
while ((length = bis.read(buffer)) > 0) {
baos.write(buffer, 0, length);
}
byte[] result = baos.toByteArray();
ImageIcon icon = new ImageIcon(result, "icon说明或标题");
System.out.println(icon.getIconWidth());
} catch (IOException ioe) {
ioe.printStackTrace();
}