packagenet.maxt.httpwatch.util;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Enumeration;importjava.util.HashMap;importjava.util.Map;importjava.util.Properties;/*** 读取Properties综合类,默认绑定到classpath下的config.properties文件。
*@author朱志杰 QQ:695520848*/
public classPropertiesUtil {//配置文件的路径
private String configPath=null;/*** 配置文件对象*/
private Properties props=null;/*** 默认构造函数,用于sh运行,自动找到classpath下的config.properties。*/
public PropertiesUtil() throwsIOException{
InputStream in= PropertiesUtil.class.getClassLoader().getResourceAsStream("config.properties");
props= newProperties();
props.load(in);//关闭资源
in.close();
}/*** 根据key值读取配置的值
* Jun 26, 2010 9:15:43 PM
*@author朱志杰
*@paramkey key值
*@returnkey 键对应的值
*@throwsIOException*/
public String readValue(String key) throwsIOException {returnprops.getProperty(key);
}/*** 读取properties的全部信息
* Jun 26, 2010 9:21:01 PM
*@author朱志杰
*@throwsFileNotFoundException 配置文件没有找到
*@throwsIOException 关闭资源文件,或者加载配置文件错误
**/
public Map readAllProperties() throwsFileNotFoundException,IOException {//保存所有的键值
Map map=new HashMap();
Enumeration en=props.propertyNames();while(en.hasMoreElements()) {
String key=(String) en.nextElement();
String Property=props.getProperty(key);
map.put(key, Property);
}returnmap;
}/*** 设置某个key的值,并保存至文件。
* Jun 26, 2010 9:15:43 PM
*@author朱志杰
*@paramkey key值
*@returnkey 键对应的值
*@throwsIOException*/
public void setValue(String key,String value) throwsIOException {
Properties prop= newProperties();
InputStream fis= new FileInputStream(this.configPath);//从输入流中读取属性列表(键和元素对)
prop.load(fis);//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(this.configPath);
prop.setProperty(key, value);//以适合使用 load 方法加载到 Properties 表中的格式,//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos,"last update");//关闭文件
fis.close();
fos.close();
}public static voidmain(String[] args) {
PropertiesUtil p;try{
p= newPropertiesUtil();
System.out.println(p.readAllProperties());
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}