2012-10-15 36 views
1

我想爲我的自定義螞蟻任務打開源文件。我認爲這很容易,因爲類加載器獲取資源肯定會立即爲我找到源文件。錯誤!閱讀自定義螞蟻任務的源文件

這裏是我的代碼:

//build the name of the template 
StringBuilder sb = new StringBuilder(VersionTemplate.class.getName()); 
sb.append(".java"); 
String templateName = sb.toString(); 
//find the template 
InputStream inputStream = VersionTemplate.class.getResourceAsStream(templateName); 

InputStream爲總是空。

任何想法?

回答

0

它發生在我之後,getResource()查找類路徑,而不是源代碼的位置。我最終只是編寫了代碼的相對路徑。

感謝您的幫助。

0

嘗試:

templateName = templateName.replaceAll(".","/"); 
    InputStream inputStream = VersionTemplate.class.getClassLoader(). 
           getResourceAsStream(templateName); 

請確保 「的.java」 文件存在於同一個包編譯 「.class」 文件。

+0

這沒有改變。使用絕對路徑是什麼意思?難道我構建模板名稱的方式不會返回類加載器可訪問的文件嗎? – Thom

+0

看看源代碼。你應該需要的一切都在那裏。 – Thom

+0

對不起,我忽略了。 **我在嘗試自己後更新了答案**。這是行得通的。請試試你的結局。 –