gpt4 book ai didi

Java-无法读取输入文件

转载 作者:行者123 更新时间:2023-12-02 12:02:59 33 4
gpt4 key购买 nike

我正在尝试将卡片的 png 图像加载到对象中,但我不断收到以下错误:

"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\lib\idea_rt.jar=60524:C:\Users\trevo\Documents\JetBrains\IntelliJ IDEA Community Edition 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\trevo\Desktop\Deck\out\production\Deck com.company.Card_Class
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
at com.company.Card_Class.main(Card_Class.java:21)

Process finished with exit code 1

这是我的代码:

package com.company;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Card_Class {
private String suit, face;
private int value;
private BufferedImage cardimage;

public Card_Class(String suit, String face, int value, BufferedImage cardimage) {
this.suit = suit;
this.face = face;
this.value = value;
this.cardimage = cardimage;
}

public static void main(String[] args) throws IOException {
Card_Class KingOfAxes = new Card_Class("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
System.out.println("King");
}
}

我将所有 png 卡文件放在标有“deck”的文件夹中,这是项目名称。

最佳答案

尝试将完整的文件路径写入控制台,看看您的文件路径是否正确。

也许将文件的绝对路径打印到标准输出以查看路径是否正确。在使用之前,您还应该检查您的图像是否存在并且可读。以下是两者的示例:

public static void main(String[] args) throws IOException {
System.out.println(new File("KingOfAxes.png").getAbsolutePath()); // Try this to pinpoint your issue
File king = new File("KingOfAxes.png");

if(king.canRead()){ // Check if your file exists and is readable before you use it
JavaAssignmentPanel KingOfAxes = new JavaAssignmentPanel("Diamonds", "King", 13, ImageIO.read(new File("KingOfAxes.png")));
} else{
throw new IOException(king.getName() + " is not readable!"); // Not readable -> Throw exception
}
System.out.println("King");
}

关于Java-无法读取输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113336/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com