码迷,mamicode.com
首页 > 其他好文 > 详细

动态读取properties文件

时间:2014-05-26 16:11:33      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:c   class   java   a   int   get   

package com.resoft.util.common;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.helpers.Loader;
/**
* 不重启动服务器动态加载properties文件改动
* @author Administrator
*
*/
public class ConfigManager {
        private static ConfigManager m_instance;
        private static String PFILE;

        synchronized public static ConfigManager getInstance() {
          if (m_instance == null) {
           if (PFILE == null) {
            URL url = Loader.getResource("kindNameChange.properties");
            PFILE = url.getPath();
//            PFILE = StringUtil.replaceAll(PFILE, "%20", " ");
           }
           m_instance = new ConfigManager();
          }
          return m_instance;
        }

        private File m_file = null;
        private long m_lastModifiedTime = 0;
        private Properties m_props = 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 {
           m_props.load(new FileInputStream(PFILE));
          } catch (Exception e) {
           e.printStackTrace();
          }
        }

        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 {
            m_props.load(new FileInputStream(PFILE));
           } catch (Exception e) {
            e.printStackTrace();
           }
          }
          m_lastModifiedTime = newTime;

          Object val = m_props.getProperty(name);
          if (val == null) {
           return defaultVal;
          } else {
           return val;
          }
        }

        }

动态读取properties文件,布布扣,bubuko.com

动态读取properties文件

标签:c   class   java   a   int   get   

原文地址:http://www.cnblogs.com/mxw272618/p/3746167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!