我正在尝试读取下面的类中的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),它可以帮助你。 –