1、哪里有servlet等class的源代码下?好想看啊,(不是API)
2、URL confURL=ConfigParser.class.getClassLoader().getResource(filename);//ConfigParser是我写的用来解析xml文件的类,就是想问问这一句中的filename究竟放在什么地方呢?我用绝对路径譬如:“E:/database.conf.xml”读取不到此文件,查了ClassLoader的源代码,
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a '/'-separated path name that identifies the resource. This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke .findResource(String) to find the resource.
看不明白如何放置此配置文件
2、URL confURL=ConfigParser.class.getClassLoader().getResource(filename);//ConfigParser是我写的用来解析xml文件的类,就是想问问这一句中的filename究竟放在什么地方呢?我用绝对路径譬如:“E:/database.conf.xml”读取不到此文件,查了ClassLoader的源代码,
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a '/'-separated path name that identifies the resource. This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke .findResource(String) to find the resource.
看不明白如何放置此配置文件
xml不懂,up
To:f_acme(沧海一声笑) ( ) 信誉:100 2006-01-06 22:46:00 得分: 0
就是我可以看到类具体是如何实现的,而不是它提供的接口方法
To:yuzl32(Hello!有酒醉) ( ) 信誉:100 2006-01-06 23:45:00 得分: 0
不行啊,我的ConfigParser如下:
public class ConfigParser extends DefaultHandler
{
private String currentSet;
private Properties props;
private String currentName;
private StringBuffer currentValue =new StringBuffer();
public ConfigParser()
{
this.props=new Properties();
}
public Properties getProps()
{
return this.props;
}
public void startElement(String uri,String localName,String qName,Attributes attributes)throws SAXException
{
currentValue.delete(0,currentValue.length());
this.currentName=qName;
}
public void characters(char[] ch, int start, int length) throws SAXException { currentValue.append(ch, start, length); } /**
*把XML配置文件的中相关的属性保存到Properties对象中
*/
public void endElement(String uri,String localName,String qName)throws SAXException
{
props.put(qName.toLowerCase(),currentValue.toString().trim());
}}另外的一个测试类中
/**
*其它程序通过ParseDatabaseConfig来获得数据库的配置信息,
*这样使得类之间的耦合松散
*/
public class ParseDatabaseConfig
{
private Properties props;
public Properties getProps()
{
return this.props;
}
/**
*解析XML配置文件,把属性保存起来
*/
public void parse(String filename)throws Exception
{
ConfigParser handler=new ConfigParser();
SAXParserFactory factory=SAXParserFactory.newInstance();
// factory.setNamespaceAware(true);
// factory.setValidating(true);
SAXParser parser=factory.newSAXParser();
URL confURL=ConfigParser.class.getClassLoader().getResource(filename);
try
{
parser.parse(confURL.toString(),handler);
props=handler.getProps();
}
finally
{
factory=null;
parser=null;
handler=null;
}
}
public static void main(String[] args) throws Exception {
ParseDatabaseConfig pd=new ParseDatabaseConfig();
pd.parse("database.conf.xml");//这一句中出现 空指针说找不到此xml文件
Properties pp=pd.getProps();
System.out.println(pp.getProperty("driver"));
}}
没有package就跟class平级
2.部推荐用ConfigParser.class.getClassLoader().getResource(filename);这个东西有缓存,有时不能在修改后即时刷新xml更新信息.在windows中路径做好用\\不要用/因为有时会出现问题.你的文件名最好不要这样database.conf.xml而是用database_conf.xml因为这里说some data (images, audio, text, etc) 是指parser不能明确你的文件类型.
2、与你包名的顶级目录平齐