-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesUtils.java
93 lines (83 loc) · 2.36 KB
/
PropertiesUtils.java
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
package com.hanshow.fresh.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* Created by tachao on 16-3-22.
*/
public class PropertiesUtils {
private static InputStream is = PropertiesUtils.class.getClassLoader().getResourceAsStream("META-INF/conf/config.properties");
private static BufferedReader bf = new BufferedReader(new InputStreamReader(is));
private static Properties prop = new Properties();
static {
try {
prop.load(bf);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取key对应的String字符串
*
* @param key
* @return
*/
public static String getString(String key) {
return prop.getProperty(key);
}
/**
* 获取key对应的String字符串,若未获取到值,则使用defaultVal作为返回值
*
* @param key
* @param defaultVal
* @return
*/
public static String getString(String key, String defaultVal) {
String value = prop.getProperty(key);
return value != null ? value : defaultVal;
}
/**
* 获取key对应的int型数字
*
* @param key
* @return
*/
public static int getInt(String key) {
String s = prop.getProperty(key);
return Integer.parseInt(s);
}
/**
* 获取key对应的int型数字,若未获取到值,则使用defaultVal作为返回值
*
* @param key
* @param defaultVal
* @return
*/
public static int getInt(String key, int defaultVal) {
String s = prop.getProperty(key);
return s != null ? Integer.parseInt(s) : defaultVal;
}
/**
* 获取key对应的Boolean型值
*
* @param key
* @return
*/
public static Boolean getBoolean(String key) {
String value = prop.getProperty(key);
return Boolean.parseBoolean(value);
}
/**
* 获取key对应的Boolean型值,若未获取到值,则使用defaultVal作为返回值
*
* @param key
* @param defaultVal
* @return
*/
public static Boolean getBoolean(String key, Boolean defaultVal) {
String value = prop.getProperty(key);
return Boolean.valueOf(value != null ? Boolean.parseBoolean(value) : defaultVal.booleanValue());
}
}