y{ //得到类的类装载器 ClassLoader loader=cls.getClassLoader(); //先从当前类所处路径的根目录中寻找属性文件 java.io.InputStream in=loader.getResourceAsStream(propFile); if(in!=null) return in; //没有找到,就从该类所处的包目录中查找属性文件 Package pack=cls.getPackage(); if(pack!=null){ String packName=pack.getName(); String path=""; if(packName.indexOf(".") < 0 ) path=packName+"/"; else{ int start=0,end=0; end=packName.indexOf("."); while(end!=-1){ path=path+packName.substring(start,end)+"/"; start=end+1; end=packName.indexOf(".",start);
} path=path+packName.substring(start)+"/"; } in=loader.getResourceAsStream(path+propFile); if(in!=null) return in; } //如果没有找到,再从当前系统的用户目录中进行查找 java.io.File f=null; String curDir=System.getProperty("user.dir"); f=new java.io.File(curDir,propFile); if(f.exists()) return new java.io.FileInputStream(f); //如果还是没有找到,则从系统所有的类路径中查找 String classpath=System.getProperty("java.class.path"); String[] cps=classpath.split(System.getProperty("path.separator")); for(int i=0;i < cps.length; i++){ f=new java.io.File(cps[i],propFile); if(f.exists()) break; f=null; } if(f!=null) return new java.io.FileInputStream(f); return null; }catch(Exception e){throw new RuntimeException(e);} } } 使用举例:利用上述的方法,可在 ConnectionPool 中自动查找和 ConnectionPool 处于 同一个包目录中的 database.properties 的输入流,并利用该输入流读入对应的属性值的代 码如下: public class ConnectionPool{ //静态初始化器,将在 ConnectionPools 加载时自动执行 static{ java.util.Properties dbProp=new java.util.Properties(); java.io.InputStream in=PropHelper.guessPropFile(edu.ec.database.ConnectionPool.class,"database.prope rties"); if(in!=null) d
bProp.load(in); //利用 dbProp,为相应的数据源对象设置相关的属性,比如 C3P0........ } }