簡體   English   中英

Java-無法讀取輸入文件

[英]Java- can't read input file

我正在嘗試將卡片的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的文件夾中,這是項目名稱。

嘗試將完整的文件路徑寫入控制台,以查看您的文件路徑是否正確。

也許將文件的絕對路徑打印到stdout,以查看路徑是否正確。 使用圖像之前,還應檢查圖像是否存在並且可讀。 這是兩個示例:

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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM