可能重複:
How do I read a resource file from a Java jar file?訪問.jar文件中的文件
開始爲谷歌搜索小時後去徹底瘋狂了這一點。我也看到了該網站上的問題的變體,但似乎無法得到它的工作。一個JFrame需要從一個ini文件中讀取數據,並且我已經創建了一個方法來打開這個文件。所述文件存儲在jar文件內稱爲資源的文件夾中。
private BufferedReader openIniFile(String filename){
BufferedReader brReader = null;
try{
File fileObj = new File(this.getClass().getResource("/resources/" + filename).toURI()); // The fileobject of the file to read
if(fileObj.exists())
brReader = new BufferedReader(new FileReader(fileObj));
} catch(Exception e){
System.err.println("Exception while opening file: " + e.getMessage());
}
return null;
}
這當然適用於編譯後運行代碼但在導出爲.jar文件後引發異常。我研究過使用InputStream,FileInputStream,但似乎無法找到可行的解決方案。
如何讓jar文件識別資源?
我試過了,不能讓它的工作:( – Chrizmo
「無法得到它的工作」不幫差不多都是這個價錢 –
對不起。它仍然拋出一個空例外 – Chrizmo