可执行jar怎么读取外部配置文件
1个回答
展开全部
先看代码目录结构: src/weather/ QueryWeather.java weather.xml 程序里面可以直接读取到weather.xml文件,代码如下: private static String getXmlContent()throws IOException { FileReader f = new FileReader("src/weather/weather.xml"); BufferedReader fb = new BufferedReader(f); StringBuffer sb = new StringBuffer(""); String s = ""; while((s = fb.readLine()) != null) { sb = sb.append(s);}return sb.toString();}但是一旦把这个class文件和xml文件打成jar包再运行,对不起,报错,QueryWeather.class字节码根本找不到weather.xml 在看打成jar包的结构:META-INFMANIFEST.MFweatherQueryWeather.class weather.xml 用下面的方法就可以找到weather.xml private static String getXmlContent()throws IOException { Reader f = new InputStreamReader(QueryWeather.class.getClass().getResourceAsStream("/weather/weather.xml")); BufferedReader fb = new BufferedReader(f); StringBuffer sb = new StringBuffer(""); String s = "";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询