如果是前者应笑拿该是没有办法的。
如果是后者。
BufferedReader br = new BufferedReader(new InputStreamReader(
((YourClass.this.getClass().getResourceAsStream("1.xml")))))
String s
while ((s = br.readLine()) != null)
System.out.println(s)
先看代码目录结构: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 = ""
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)