我正在尝试创建一个具有 png 图像属性的对象。我测试图像是否可读的代码。我的代码告诉我图像不可读。有没有办法使图像可读或者我需要找到一个新图像?
System.out.println(new File("king_of_hearts.png").getAbsolutePath()); // Try this to pinpoint your issue
File king = new File("king_of_hearts.png");
if(king.canRead()){ // Check if your file exists and is readable before you use it
BufferedImage KingOfAxes = ImageIO.read(new File("king_of_hearts.png"));
} else{
throw new IOException(king.getName() + " is not readable!"); // Not readable -> Throw exception
}
System.out.println("King");
结果是这样的:
C:\Users\trevo\Desktop\Testcode\king_of_hearts.png
Exception in thread "main" java.io.IOException: king_of_hearts.png is not readable!
at com.company.Main.main(Main.java:20)
Process finished with exit code 1