liurunyu
2024-04-15 55d5046ae0a9da1109e402d7d719f765a4bccdae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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()) ;
         }
    }
}