java 读取配置文件(二)

package com.xxx.config;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * 读配置集成类<br>
 * 如果不设置配置源,默认使用xxxxxx的配置
 * @author 
 */
public class ConfigManager {
	private static Configs _default = null;
	//private static HashMap configs = new HashMap();
	private static boolean hasdefault = true;
	
    /**
     * 得到指定配置文件的配置信息
     * @param conf 配置文件名
     * @return Configs对象
     */
    public static Configs getConfigs(String module) {
    	Configs cf = null;//(Configs) configs.get(module);
    	
    	if (cf == null) {
	    	Configs df = getDefaultConfigs();
	    	if (df == null)
	    		return null;
	
	    	String file = df.getConfig(module);
	    	if (file != null && file.length() > 0 && new java.io.File(file).isFile()) {
		    	cf = new Configs();
		    	cf.init(file, df.getConfigProperty());
	    	} else {
	    		cf = df;
	    	}
	    	//configs.put(module, cf);
    	}
        return cf;
    }

    /**
     * 得到指定配置文件的配置信息
     * @param conf 配置文件名
     * @return Configs对象
     */
    public static void setDefault(String module) {
    	Configs df = getDefaultConfigs();
    	if (df == null)
    		return;

    	String file = df.getConfig(module);
    	if (file != null && file.length() > 0 && new java.io.File(file).isFile()) {
    		Configs cf = new Configs();
	    	cf.init(file, df.getConfigProperty());
	    	_default = cf;
    	}
    }

    /**
     * 得到指定配置文件的配置信息
     * @param conf 配置文件名
     * @return Configs对象
     */
    public static void resetDefault() {
    	_default = null;
    }

    /**
     * 根据配置节点名得到配置值
     * @param key 配置节点名
     * @return 配置值
     */
    public static String getKey(String key) {
        return getKey(key, null, false);
    }

    /**
     * 根据配置节点名得到配置值
     * @param key 配置节点名
     * @param defaul 默认值
     * @return 如果有配置值,则返回配置值,否则返回默认值
     */
    public static String getKey(String key, String defaul) {
        return getKey(key, defaul, false);
    }

    /**
     * 根据配置节点名得到配置值
     * @param key 配置节点名
     * @param islog 是否写日志。如果为true且返回的配置值为null,则写日志
     * @return 配置值
     */
    public static String getKey(String key, boolean islog) {
        return getKey(key, null, islog);
    }

    /**
     * 根据配置节点名得到配置值
     * @param key 配置节点名
     * @param defaul 默认值
     * @param islog 是否写日志。如果为true且返回的配置值为null,则写日志
     * @return 如果有配置值,则返回配置值,否则返回默认值
     */
    public static String getKey(String key, String defaul, boolean islog) {
        Configs cf = getDefaultConfigs();
        if (cf != null)
        	return cf.getConfig(key, defaul, islog);
        
        return defaul;
    }

    /**
     * 得到默认配置
     * @return Configs对象
     */
    public static Configs getDefaultConfigs() {
    	if (_default == null && hasdefault) {
    		loadDefaultConfig();
    	}
    	return _default;
    }

    static void loadDefaultConfig() {
        String f = "";
        if(File.separatorChar == '\\')
            f = "C:\\xxx.conf";
        else
            f = "/xxx.conf";
        
        System.out.println("配置文件地址"+f+"---------------------");
        
        File file = new File(f);
        if (!file.isFile()) {
        	hasdefault = false;
        	return;
        }

        _default = new Configs();
        _default.init(f);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值