2014-01-14 58 views
0

我需要從戰爭中獲取屬性文件。無法從戰爭中獲取文件

String fileName = loadFileName(); 
try { 
    FileInputStream fis = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource(fileName).toURI())); 
    property.load(fis); 
} 
......... 

private String loadFileName(){ 
    if(currentLocale.equals(new Locale("en"))){ 
     return "file:///locale/output/language.properties"; 
    } else { 
     return "file:///locale/output/language_ru.properties"; 
    } 
} 

此文件位於main/resources/locale/output文件夾中。不幸的是,這種方法不起作用,所有的屬性值都是空的。如何正確加載戰爭文件?服務器:JBoss應用服務器7

enter image description here

+0

如果資源位於正確位置,請直接使用'getResourceAsStream(fileName)'讀取'InputStream'。 –

+0

查看您的代碼,您可能會發現使用[ResourceBundle](http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html)更容易。 –

+0

@SotiriosDelimanolis它沒有幫助。也許我的路線錯了?我添加完整路徑的圖片。 –

回答

1

一戰是一個存檔文件。您無法將存檔中的「文件」作爲文件尋址。只需通過#getResourceAsStream(String name)打開資源即可。 name大概是/locale/output/language.properties這裏。

當然,你不應該自己創建i18n。

+0

謝謝!現在正在工作。 –