把xml文件放在什么路径下面?谢谢!!!

llyjava 2003-09-03 05:30:41
用jb8,现在有一个a1.xml文件,想使用以下代码:
SAXBuilder sb=new SAXBuilder();
InputStream in = Jsp1Bean.class.getResourceAsStream("a1.xml");
Document doc=sb.build(in);
不知道把a1.xml放在什么地方?哪个路径下面?谢谢了!!!
...全文
282 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
广州秀才 2004-01-15
  • 打赏
  • 举报
回复
private static String sConfigFileName = "Config.xml";
//得到配置文件Config.xml所在目录. tomcat中是在WEB-INF/classes
URL confURL = null;
if (confURL == null)
confURL = ConfigParserXML.class.getClassLoader().getResource(sConfigFileName);
if (confURL == null)
confURL = ConfigParserXML.class.getClassLoader().getResource("/" + sConfigFileName);
if (confURL == null)
confURL = Thread.currentThread().getContextClassLoader().getResource(sConfigFileName);
if (confURL == null)
confURL = ClassLoader.getSystemResource(sConfigFileName);
System.out.println(confURL);


输出的confURL即为相对路径
file:/D:/Personal/Test/classes/Config.xml

注:ConfigParserXML为当前类的类名
Leemaasn 2004-01-15
  • 打赏
  • 举报
回复
你那是编译错误,不是运行错误,
没关系的。

加上try就可以了。


相对路径,你可以放到和你的程序同一个目录下。。。
hlf566 2004-01-15
  • 打赏
  • 举报
回复
加try catch即可
package com.inrun.webapp.bean;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.io.FileInputStream;
public class Jsp1Bean {
public void opXML(){
try { SAXBuilder sb=new SAXBuilder();
FileInputStream in=new FileInputStream("f:\\a1.xml");
Document doc=sb.build(in);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
llyjava 2003-09-03
  • 打赏
  • 举报
回复
相对路径怎么放啊!没有人告诉我啊?
llyjava 2003-09-03
  • 打赏
  • 举报
回复
up
llyjava 2003-09-03
  • 打赏
  • 举报
回复
我的代码是这样的:
package com.inrun.webapp.bean;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.io.FileInputStream;
public class Jsp1Bean {
public void opXML(){
SAXBuilder sb=new SAXBuilder();
FileInputStream in=new FileInputStream("f:\\a1.xml");
Document doc=sb.build(in);
}
}

报错:
unreported exception: java.io.FileNotFoundException; must be caught or declared to be thrown at line 33, column 26
leshui 2003-09-03
  • 打赏
  • 举报
回复
放到哪里都行,
只要你把绝对路径指给它
InputStream in = Jsp1Bean.class.getResourceAsStream("c:\\a1.xml");
leshui 2003-09-03
  • 打赏
  • 举报
回复
放到哪里都行,
只要你把绝对路径指给它
InputStream in = Jsp1Bean.class.getResourceAsStream("c:\\a1.xml");
leshui 2003-09-03
  • 打赏
  • 举报
回复
放到哪里都行,
只要你把绝对路径指给它
InputStream in = Jsp1Bean.class.getResourceAsStream("c:\\a1.xml");

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧