1. 配置文件 myconf.properties
url=www.baidu.com
2. 类文件 MyProperties.class
public class MyConf {
public static Properties properties = new Properties();
public static String url= getPropertieVal("url");
public static String getPropertieVal(String propertype){
if(properties==null || properties.size()==0) readProperties();
return properties.getProperty(propertype);
}
public static void readProperties() {
try {
properties.load(MyConf.class.getResourceAsStream("/myconf.properties"));
}catch(FileNotFoundException e){
System.out.println("Error read");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
3. 调用
Class Test {
public static void main(String[] args) {
System.out.println(MyConf.url);
}
}