| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.common.util; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.io.InputStream; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.Iterator; | 
|---|
|  |  |  | import java.util.Map.Entry; | 
|---|
|  |  |  | import java.util.Properties; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @SuppressWarnings("unused") | 
|---|
|  |  |  | public class ConfigProperties { | 
|---|
|  |  |  | private static HashMap<String, String> properties = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private ConfigProperties() { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 是否有属性了 | 
|---|
|  |  |  | * @return 存在属性配置返回true,否则返回false | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean hasProperties(){ | 
|---|
|  |  |  | return properties != null && properties.size() > 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据配置名称得到配置值 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param name 属性名 | 
|---|
|  |  |  | * @return 属性值 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String getConfig(String name) { | 
|---|
|  |  |  | if (properties == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(name.trim().equals("")){ | 
|---|
|  |  |  | return "" ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return properties.get(name); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化配置 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param in 输入流 | 
|---|
|  |  |  | * @param repeatCover 是否重复 | 
|---|
|  |  |  | * @throws Exception  抛出异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void init(InputStream in, boolean repeatCover) throws Exception { | 
|---|
|  |  |  | if(in == null){ | 
|---|
|  |  |  | throw new Exception("属性配置文件不存在!") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(properties.size() > 0){ | 
|---|
|  |  |  | if(repeatCover){ | 
|---|
|  |  |  | parseProperties(in); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | parseProperties(in); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 重新初始化 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param in 输入流 | 
|---|
|  |  |  | * @throws Exception 抛出异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void reInit(InputStream in) throws Exception { | 
|---|
|  |  |  | properties = new HashMap<>(); | 
|---|
|  |  |  | parseProperties(in); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static void parseProperties(InputStream in) throws IOException{ | 
|---|
|  |  |  | Properties prop = new Properties(); | 
|---|
|  |  |  | prop.load(in); | 
|---|
|  |  |  | Iterator<Entry<Object, Object>> it = prop.entrySet().iterator() ; | 
|---|
|  |  |  | Entry<Object, Object> entry ; | 
|---|
|  |  |  | while(it.hasNext()){ | 
|---|
|  |  |  | entry = it.next() ; | 
|---|
|  |  |  | properties.put((String)entry.getKey(), (String)entry.getValue()) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|