读配置文件(properties)ConfigFileUtils

来源:互联网 发布:移动设备数据恢复 编辑:程序博客网 时间:2024/06/04 19:21
/** *  */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
原创粉丝点击