InitConf.java

package com.citi.dashboard;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class InitConf {
	public static List<String> getConf(String file) throws Exception{
		System.setProperty("file.encoding", "UTF-8");
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		InputStream is = classLoader.getResourceAsStream(file);
		BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		String line = "";

		List<String> list = new ArrayList<String>();
		while((line = br.readLine()) != null) {
			String sig = new String("#");
			if(line.startsWith(sig))
				continue;
			line = line.replaceAll("\\\\", "/");
			list.add(line);
			System.out.println(line);
		}
		return list;
	}
	
	public static Map<String, String> getProperties(String relativePath) throws Exception {
		System.setProperty("file.encoding", "UTF-8");
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		InputStream is = classLoader.getResourceAsStream(relativePath);
		Properties prop = new Properties();
		prop.load(is);
		Iterator<Entry<Object, Object>> it = prop.entrySet().iterator();
		Map<String, String> map = new HashMap<String, String>();
		while(it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			map.put(entry.getKey().toString(), entry.getValue().toString());
		}
		return map;
	}
	
	public static String getProperty(String relativePath, String key) throws Exception {
		System.setProperty("file.encoding", "UTF-8");
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		InputStream is = classLoader.getResourceAsStream(relativePath);
		Properties prop = new Properties();
		prop.load(is);
		return prop.getProperty(key);
	}
	
	public static void main(String[] args) throws Exception{
		Map<String, String> map = getProperties("conf/elementConf.properties");
		for(Entry<String, String> entry: map.entrySet()) {
			System.out.println(entry.getKey() + " = " + entry.getValue()); 
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值