2017-11-04 10 views
2

を読み取ることができません。ここでJava対応は私がオブジェクトにカードの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"); 
    } 
} 

プロジェクト名であるdeckというラベルの付いたフォルダにすべてのPNGカードファイルがあります。

+1

ログイメージの代わりに実際のテキストスタックトレースログを追加します。画像を削除してください:) –

+0

@VikrantKashyap私は正しくフォーマットしたかどうかわかりません。 – user8735495

答えて

1

完全なファイルパスをコンソールに書き込んで、ファイルパスが正しいかどうかを確認してください。

ファイルの絶対パスを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"); 
} 
+0

Philさん、ありがとう、これはうまくいった。私のファイルは読めません。どのように私はそれを読みやすくすることができますか? – user8735495

+0

オペレーティングシステム(たとえば、Linuxのchmod)でファイルのアクセス権を変更することも、プログラム的に以下のようにすることもできます。 https://stackoverflow.com/a/32331442/8883654 –

関連する問題