java管理properties

package com.pubinfo.core.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
 * 属性管理器
 * @ClassName : ConfigManager
 * @Description : TODO 属性管理器,通过单例模式实现属性配置文件的加载,属性的添加和删除等功能;修改属性配置文件以后,免重启服务器,即刻生效
 * @Author : lvhonglei
 * @Date : 2015年3月3日 上午9:20:06
 * @Version : 1.0
 * @Copyright : 
 */
public class ConfigManager {
 //配置文件路径,这个地方可以有更灵活的配置方式
 private static final String PFILE=Thread.currentThread().getContextClassLoader().getResource("config.properties").getPath().replaceAll("%20", " ");
 //属性文件对应的文件变量
 private File m_file=null;
 //属性文件最后修改日期
 private long m_lastModifiedTime = 0;
 //属性文件对应的属性变量
 private Properties m_props = null;
 //本类存在的唯一实例
 private static ConfigManager m_instance = new ConfigManager();
 private FileInputStream fis=null;
 private FileOutputStream fos = null;
 /**
  * 私有构造方法
  */
 private ConfigManager(){
  m_file = new File(PFILE);
  m_lastModifiedTime = m_file.lastModified();
  if(m_lastModifiedTime == 0){
   System.err.println(PFILE+"File does not exist!");
  }
  m_props = new Properties();
  
  try {
   fis = new FileInputStream(PFILE);
   m_props.load(fis);
   fis.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 /**
  * 
  * @MethodName : getInstance
  * @Description : TODO 同步静态工厂方法
  * @Return Type : ConfigManager
  * @Author : lvhonglei
  * @Date : 2015年3月3日 上午9:52:38
  *
  */
 synchronized public static ConfigManager getInstance(){
  return m_instance;
 }
 
 /**
  * 
  * @MethodName : getConfigItem
  * @Description : TODO 读取一个特定的属性项
  * @Return Type : Object
  * @Author : lvhonglei
  * @Date : 2015年3月3日 上午9:55:07
  *
  */
 final public Object getConfigItem(String name,Object defaultVal){
  long newTime = m_file.lastModified();
  if(newTime ==0){
   if(m_lastModifiedTime==0){
    System.err.println(PFILE+"file does not exist!");
   }else{
    System.err.println(PFILE+"file was deleted!");
   }
   return defaultVal;
  }else if(newTime > m_lastModifiedTime){
   m_props.clear();
   
   try {
    fis = new FileInputStream(PFILE);
    m_props.load(fis);
    fis.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
  m_lastModifiedTime = newTime;
  Object val = m_props.getProperty(name);
  if(val == null){
   return defaultVal;
  }else{
   return val;
  }
 }
 
 /**
  * 
  * @MethodName : addConfigItem
  * @Description : TODO 添加属性
  * @Return Type : String
  * @Author : lvhonglei
  * @Date : 2015年3月3日 下午3:06:36
  *
  */
 final public String addConfigItem(String key,String value){
  String message="";
  if(m_props.containsKey(key)){
   message = "KEY已经存在文件中";
   System.out.println(m_props.size());
  }else{
   try {
    fos = new FileOutputStream(PFILE);
    m_props.put(key, value);
    m_props.store(fos, "lvhonglei:123");
    message = "添加成功";
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return message;
 }
 
 /**
  * 
  * @MethodName : deleteConfigItem
  * @Description : TODO 删除属性方法
  * @Return Type : String
  * @Author : lvhonglei
  * @Date : 2015年3月3日 下午4:29:01
  *
  */
 final public String deleteConfigItem(String key){
  String message = "";
  if(m_props.containsKey(key)){
   try {
    fos = new FileOutputStream(PFILE);
    m_props.remove(key);
    m_props.store(fos, "delete by lvhonglei");
    message = "删除属性成功!";
    fos.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }else{
   message = "属性文件中没有此属性";
  }
  return message;
 }
 
 
 public static void main(String[] args) {
  String newKey = ConfigManager.getInstance().addConfigItem("99","中国人");
  System.out.println(newKey);
  String key = (String) ConfigManager.getInstance().getConfigItem("99", "not found");
  System.out.println(key);
  
 }
}

使用方法,main中的调用方式

你可能感兴趣的:(java,单例模式,properties)