楼上的,非常谢谢,但我按照你写的例子做,但不行,props.load(this.getClassLoader().getClass().getResourceAsStream,这行报错,代码如下: public class test { public static void main(String[] args) { Properties props = new Properties(); props.load(this.getClassLoader().getClass().getResourceAsStream("/power/config.properties")); String mainlog_dir=""; mainlog_dir=props.getProperty("mainlog_dir"); System.out.println("mainlog_dir=["+mainlog_dir+"]");
还是报错,错误如下: java.io.FileNotFoundException: D:\Program Files\eclipse3.1\workspace\JasonLong\src\test\test\config.properties (系统找不到指定的路径。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at test.test.main(test.java:27)
public class PropertiesConfig{ public static final String module = PropertiesConfig.class.getName(); public static final Properties prop = new Properties(); private static PropertiesConfig propertiesConfig= null; private PropertiesConfig(){
}; synchronized public PropertiesConfig getPropInstance(){ if(propertiesConfig==null){ propertiesConfig = new PropertiesConfig() ; } return propertiesConfig; } public static void loadProperties(){ try{ File f = new File("."); String path = f.getAbsolutePath(); path = path.substring(0,path.length()-1)+"config.Properties"; //System.out.println(path); InputStream inputStream = new FileInputStream(path); prop.load(inputStream); }catch(FileNotFoundException e){ System.out.println("[File NOT FOUND Exception]"+e.toString()); }catch(IOException e){ System.out.println(e.toString()); }
} public static Properties getProperties(){ loadProperties(); return prop; } public static void main(String[] args){ //Debug.logInfo(module); System.out.println(module);
ht.put("mainlog_dir",new String("D:/ProgramFiles/eclipse3.1/workspace/JasonLong/src/power/log/"));
....如果是xml文件用jndi来读一般配置信息是用.perperty文件存放,然后用读取Property信息
props.load(this.getClassLoader().getClass().getResourceAsStream("/config/config.properties"));
String mainlog_dir="";
mainlog_dir=props.getProperty("mainlog_dir");/config/config.properties这个文件里面应该有下面的内容
mainlog_dir=D:/Program Files/eclipse3.1/workspace/JasonLong/src/power/log/
forname=org.postgresql.Driver
dbsource=jdbc:postgresql://192.168.1.10:5432/powerdb
username=postgre
passwd=postgre
findfie_interval=5
data_dir=D:、data/power/
backup_dir=D:、data/
public class test { public static void main(String[] args) {
Properties props = new Properties();
props.load(this.getClassLoader().getClass().getResourceAsStream("/power/config.properties"));
String mainlog_dir="";
mainlog_dir=props.getProperty("mainlog_dir"); System.out.println("mainlog_dir=["+mainlog_dir+"]");
}
}
java.io.FileNotFoundException: D:\Program Files\eclipse3.1\workspace\JasonLong\src\test\test\config.properties (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at test.test.main(test.java:27)
你要确定/power/config.properties是对的
public static final String module = PropertiesConfig.class.getName();
public static final Properties prop = new Properties();
private static PropertiesConfig propertiesConfig= null;
private PropertiesConfig(){
};
synchronized public PropertiesConfig getPropInstance(){
if(propertiesConfig==null){
propertiesConfig = new PropertiesConfig() ;
}
return propertiesConfig;
}
public static void loadProperties(){
try{
File f = new File(".");
String path = f.getAbsolutePath();
path = path.substring(0,path.length()-1)+"config.Properties";
//System.out.println(path);
InputStream inputStream = new FileInputStream(path);
prop.load(inputStream);
}catch(FileNotFoundException e){
System.out.println("[File NOT FOUND Exception]"+e.toString());
}catch(IOException e){
System.out.println(e.toString());
}
}
public static Properties getProperties(){
loadProperties();
return prop;
}
public static void main(String[] args){
//Debug.logInfo(module);
System.out.println(module);
loadProperties();
System.out.println((String)prop.get("CONTROLLER_XML"));
}
}