最近在弄项目,由于项目的特殊性出现测试环境和生产环境存在很多的参数配置发生改变,从一开始的一个个配置参数的修改的繁琐操作到现在的配置文件的方式,大大提高了发布效率,现在将这这个进步过程中使用到的properties配置方式做了一个简单的总结分为三种情况如下:
①这种方式是读取在jar包外面的配置文件的路径:
public static void main(String[] args) {
try{
InputStream in=new BufferedInputStream(new FileInputStream("resource/properties/test.properties"));
Properties p=new Properties();
p.load(in);
System.out.println("properties:"+p.getProperty("name")+","+p.getProperty("age"));
if(in!=null)
{
in.close();
}
}catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}private GetConfig(){
try{
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties");
prop.load(inputStream);
this.driverName = prop.getProperty("driverName","");
}catch (Exception e) {
e.printStackTrace();
}
}
③读取src下面包内的配置文件
private GetConfig(){
try{
Properties prop = new Properties();
InputStream inputStream=this.getClass().getResourceAsStream("/com/haiqi/config/Config.properties");
prop.load(inputStream);
this.driverName = prop.getProperty("driverName","");
}catch (Exception e) {
e.printStackTrace();
}
}
原文:http://blog.csdn.net/u010488222/article/details/45391081