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()); } } }
InitConf.java
最新推荐文章于 2023-12-21 15:36:33 发布