properties 配置文件相关

package com.chb.abc.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class PropertiesUtil {

	static Map<String, Map> oMap = new HashMap<String, Map>();

	public static Map getProperties(String path) {
		Properties prop = new Properties();
		Map<String, String> map = new HashMap<String, String>();
		if (path == null) {
			return null;
		}
		
		if (!path.substring(0, 1).equals("/")) {
			path = "/" + path;
		}
		if (oMap.get(path) != null) {
			return oMap.get(path);
		}
		InputStream in = Object.class.getResourceAsStream(path);
		try {
			prop.load(in);
			Iterator<Entry<Object, Object>> it = prop.entrySet().iterator();
			while (it.hasNext()) {
				Entry<Object, Object> entry = it.next();
				map.put(entry.getKey().toString(), entry.getValue().toString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			return null;
		}
		System.out.println("-----------------------");
		oMap.put(path, map);
		return map;
	}
	public static void main(String[] args) {
		PropertiesUtil.getProperties("msgConfig.properties");
		PropertiesUtil.getProperties("msgConfig.properties");
	}
}
<pre name="code" class="java">package com.chb.abc.common.util;

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

/**
 * properties文件工具类
 */
public class PropertiesUtil {
	public static Properties getProperty(String path){
		Properties pro = new Properties();
		InputStream input =PropertiesUtil.class.getResourceAsStream(path);
		try {
			pro.load(input);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return pro;
	}
	
	public static String getString(String key,String path){
		if(getProperty(path).containsKey(key)){
			return getProperty(path).getProperty(key);
		}
		return null;
	}
	
	public static int getInt(String key,String path){
		try {
			String s = getString(key, path);
			if(s!=null){
				return Integer.parseInt(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值