peternameyakj
2025-01-06 4c82733d79b03ee1d5304398b0598d826e6fd0e9
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package apache.poi.extracto;
 
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class PropertiesUtil {
    
    private static Properties prop = null;
    
    private static Log log = LogFactory.getLog(PropertiesUtil.class);
    
    private static String CONFIG_FILENAME = "spring/character.properties";
    
    public PropertiesUtil() {
        if (prop == null) {
            loadProperties();
        }
    }
    
    /**
     * 装载属性文件
     */
    private synchronized static void loadProperties() {
        try {
            InputStream is=PropertiesUtil.class.getResourceAsStream("/" + CONFIG_FILENAME);
            prop = new Properties();
            prop.load(is);
        }catch (Exception e) {
            System.err.println("读取配置文件失败!!!");
            prop = null;
            log.error(e.getMessage(), e);
        }
    }
    
    /**
     * 得到属性值
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        if (prop == null) loadProperties();
        String value = prop.getProperty(key);
        if(value ==null) return null;
        return value.trim();    
    }
    
    /**
     * 得到内容包含汉字的属性值
     * @param key
     * @return
     */
    public static String getGBKProperty(String key) {
        String value = getProperty(key);
        
        try {
            value = new String(value.getBytes("ISO8859-1"),"GBK");
        } catch (UnsupportedEncodingException e) {}
        
        return null==value?null:value.trim();
    }
    
    /**
     * 得到属性值
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {
        if (prop == null) loadProperties();
        String value = prop.getProperty(key, defaultValue);
        return null==value?null:value.trim();
    }
    
    /**
     * 得到内容包含汉字的属性值,如果不存在则使用默认值
     * @param key
     * @return
     */
    public static String getGBKProperty(String key, String defaultValue) {
        try {
            defaultValue = new String(defaultValue.getBytes("GBK"), "ISO8859-1");
            String value = getProperty(key, defaultValue);
            return new String(value.getBytes("ISO8859-1"), "GBK").trim();
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    
    public static String getUTFProperty(String key, String defaultValue) {
        try {
            defaultValue = new String(defaultValue.getBytes("UTF-8"),"ISO8859-1");
            String value = getProperty(key, defaultValue);
            value = new String(value.getBytes("ISO8859-1"), "UTF-8");
            return null==value?null:value.trim();
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}