读取配置文件

 

 

/**
     * 读取数据库配置私有参数值
     *
     * @return
     */
    public static Properties getDBFacelibPropConfig() {
        if (null == dbFaceCloudConfig) {
            try {
                dbFaceCloudConfig = new Properties();
                URL url = FaceCloudConfig.class.getClassLoader().getResource(FaceCloudConstant.DB_FACECLOUD_CONFIG_NAME);
                LOG.info(url.toString());
                String facelibConfigPath = FaceCloudConfig.class.getClassLoader()
                        .getResource(FaceCloudConstant.DB_FACECLOUD_CONFIG_NAME).toURI().getPath();
                dbFaceCloudConfig = PropertiesUtil.getProperties(facelibConfigPath);
                //                }
            } catch (Exception e) {
                LOG.error("*********************************************");
                LOG.error("error appear in FacelibConfig getDBFacelibPropConfig method :", e);
                LOG.error("*********************************************");
            }
        }
        return dbFaceCloudConfig;
    }
PropertiesUtil:
/**
 * @ProjectName: BSP 海康威视大数据服务平台
 * @Copyright: 2015 HangZhou Hikvision System Technology Co., Ltd. All Right Reserved.
 * @address: http://www.hikvision.com
 * @date: 2015年6月27日 下午2:36:45
 * @Description: 本内容仅限于杭州海康威视数字技术股份有限公司内部使用,禁止转发.
 */
package com.hikvision.bsp.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Properties配置文件工具类
 * 
 * @author zhuchao 2015年6月27日 下午2:36:45
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: $author$ $date$
 * @modify by reason:{方法名}:{原因}
 */
public final class PropertiesUtil {
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);

    /**
     * 修改属性文件
     * 
     * @author zhuchao 2015年6月27日 下午2:37:14
     * @param keys
     * @param values
     * @param filePath
     */
    public static void updateProperties(Map<String, String> pros, String filePath) {
        try {
            // pros为空则返回
            if (pros == null || pros.size() == 0) {
                return;
            }
            if (StringUtil.isBlank(filePath)) {
                LOG.error("传入配置文件路径不能为空.");
                return;
            }
            File file = new File(filePath);
            if (!file.exists() || !file.isFile()) {
                LOG.error("传入配置文件路径:{}不正确", filePath);
                return;
            }
            PropertiesConfiguration pro = new PropertiesConfiguration(file);
            for (Entry<String, String> entry : pros.entrySet()) {
                String value = entry.getValue();
                if (null == value) {
                    value = "";
                }
                pro.setProperty(entry.getKey(), value);
            }
            pro.save();
        } catch (ConfigurationException e) {
            LOG.error("修改配置文件出错", e);
        }
    }

    /**
     * 读取配置文件
     * 
     * @author zhuchao 2015年6月27日 下午2:37:27
     * @param file
     * @return
     */
    public static Properties getProperties(String file) {
        Properties pro = null;
        // 从文件mdxbu.properties中读取网元ID和模块ID信息
        FileInputStream in = null;
        try {
            in = new FileInputStream(new File(file));
            pro = new Properties();
            pro.load(in);
        } catch (Exception e) {
            LOG.error("Read " + file + " IOException:", e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                LOG.error("Read " + file + " IOException:", e);
            }
        }
        return pro;
    }
}

 

转载于:https://www.cnblogs.com/xjatj/p/9262948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值