我正在嘗試讀取下面的類中的xml配置。它運行時運行它。但是當我將它導出到Runnable Jar.The文件不被讀取..? ?如何使用getResourceAsStream讀取XML文件
public KeyTranslator() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String sPath = "res/config.xml";
//InputStream is = (InputStream) Thread.currentThread().getContextClassLoader().getResourceAsStream(sPath);
//InputStream is = this.getClass().getResourceAsStream((sPath);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
if (AppFrame.jar == null) {
this.myDoc = builder.parse(new File((AppFrame.basePath+sPath).replace('\\', '/')));//ensure seperator is '/' as linux is picky
} else {
this.myDoc = builder.parse(AppFrame.jar.getInputStream(AppFrame.jar.getJarEntry(sPath)));
}
}
我GOOGLE了&發現的getResourceAsStream method.But似乎拋出FileNotFoundException異常。 &我不知道如何在我的代碼中添加InputSream?
所以幫我在正確的方向:)
感謝您的幫助......
注意 我試過的方法進行了報道
你加罐子類路徑? – lakshman
看看[這](https://stackoverflow.com/questions/20963531/why-do-i-need-to-include-classloader-getsystemresourcesasstream-when-parsing-a/20963663#20963663),它可以幫助你。 –