2017-07-27 47 views
0

我想檢查具有已定義路徑的javafx映像是否存在。 如果在文件系統中不存在jet,我不想加載它。 但要做到這一點,我必須先檢查路徑。 我需要知道是否有一個函數來驗證它。如何驗證javafx映像的路徑

這裏就是我想要創建圖像的構造:

public TestController(String title){ 
    this.model = title; 
    this.modelurl = "images/Test/"+model+"/uebersicht.png"; 
    this.farbImg = new Image(modelurl, 1500, 900, true, true); 
} 

有誰知道一個聰明的辦法來做到這一點?

+0

如果路徑無效/不存在,會發生什麼情況? – Calips

+0

@Calips 嗨再次:) 然後這個異常將被拋出: 引起:java.lang.IllegalArgumentException:無效的URL:無效的URL或資源未找到 – pR4yChaos

+0

再次嗨! :P然後你得到了你的答案:)你需要捕獲該異常:D,因爲基本上圖像不會加載,因爲路徑不存在:D – Calips

回答

1
try{ 
    this.farbImg = new Image(modelurl, 1500, 900, true, true); 
} catch (Exception e){ //Change the "Exception" class by the adequate exception 
    //URL Not valid or whatever exception it caught 
    //Do something 
    System.out.println("Exception thrown :" + e); 
} 

儘管我沒有IDE在這裏測試,但我認爲這將是一個IOException。