2017-11-04 1 views
2

입력 파일을 읽을 수 있지만 다음과 같은 오류가 점점 계속 :자바 - 내가 객체에 카드의 PNG로 이미지를로드하려고

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

내 코드입니다

프로젝트 이름 인 deck라는 폴더에 내 모든 png 카드 파일이 있습니다.

+1

로그 이미지 대신 실제 텍스트 스택 추적 로그를 추가하십시오. 이미지를 제거하십시오 :) –

+0

@VikrantKashyap 만약 내가 제대로 포맷 모르겠어요. – user8735495

답변

1

전체 파일 경로를 콘솔에 작성하여 파일 경로가 올바른지 확인하십시오.

경로가 맞는지 확인하기 위해 파일의 절대 경로를 표준 출력으로 인쇄 할 수 있습니다. 이미지를 사용하기 전에 이미지가 존재하고 읽을 수 있는지 확인해야합니다. 다음은 모두에 대한 예제입니다.

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 –

관련 문제