读配置文件(properties)ConfigFileUtils

/**
 * 
 */
package com.sprucetec.tms.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.core.io.support.PropertiesLoaderUtils;


/**
 * 
 * Title: ConfigFileUtils.java<br>
 * Description: <br>
 * Copyright: Copyright (c) 2016<br>
 * Company: 北京云杉世界信息技术有限公司<br>
 * 
 * @author MiaoShun
 * 2016年3月2日
 */

public class TmsConfigFileUtils {
    private static final Logger logger = Logger.getLogger(TmsConfigFileUtils.class);
    /**
     * 
     * @author meicai
     * 2016年3月2日
     * @param nodeName
     * @return
     */
    public static ArrayList<Long> getShardconfig(String nodeName) {
        String configName = "shard.properties";
        String result = pubReadConfig(configName, nodeName);
        if (result == null) {
            result = pubReadConfig("prop/" + configName, nodeName);
        }
        String[] nodes = result.split(",");
        ArrayList<Long> nodeList = new ArrayList<Long>();
        for(String node : nodes){
            nodeList.add(Long.valueOf(node));
        }
        return nodeList;
    }

    /**
     * 通用读取方法
     * 
     * @param configName
     * @param nodeName
     * @return
     */
    public static String pubReadConfig(String configName, String nodeName) {
        Properties props = new Properties();
        while (true) {
            try {
                props = PropertiesLoaderUtils.loadAllProperties(configName);
                return (String) props.get(nodeName);
            } catch (IOException e) {
                logger.error("ReadConfigException", e);
                System.out.println(e.getMessage());
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class ReadConfigation{ /** * 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于属性文件的文件对象变量 */ private File m_file = null; /** * 属性文件的最后修改日期 */ private long m_lastModifiedTime = 0; /** * 属性文件所对应的属性对象变量 */ private Properties m_props = null; /** * 本类可能存在的惟一的一个实例 */ private static ReadConfigation m_instance =new ReadConfigation(); /** * 私有的构造子,用以保证外界无法直接实例化 */ private ReadConfigation() { 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(); } } /** * 静态工厂方法 * @return 返还ReadConfigation 类的单一实例 */ synchronized public static ReadConfigation getInstance() { return m_instance; } /** * 取一特定的属性项 * * @param name 属性项的项名 * @param defaultVal 属性项的默认值 * @return 属性项的值(如此项存在), 默认值(如此项不存在) */ public String getConfigItem(String name,String 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){ // Get rid of the old properties m_props.clear(); try { m_props.load(new FileInputStream(PFILE)); }catch(Exception e){ e.printStackTrace(); } } m_lastModifiedTime = newTime; String val = m_props.getProperty(name); if( val == null ) { return defaultVal; } else { return val; } } /** * 取一特定的属性项 * * @param name 属性项的项名 * @return 属性项的值(如此项存在), 空(如此项不存在) */ public String getConfigItem(String name){ return getConfigItem(name,""); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值