-
Notifications
You must be signed in to change notification settings - Fork 1
/
PropertyUtil.java
198 lines (185 loc) · 5.51 KB
/
PropertyUtil.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cn.xanderye.util;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.Optional;
import java.util.Properties;
/**
* 配置工具类
*
* @author XanderYe
* @date 2020/3/15
*/
@Slf4j
public class PropertyUtil {
private static volatile Properties properties = null;
private static String filePath = null;
/**
* 初始化
*
* @return void
* @author XanderYe
* @date 2020-03-15
*/
public static void init() {
init(null);
}
/**
* 初始化
*
* @param path
* @return void
* @author XanderYe
* @date 2020-03-15
*/
public static void init(String path) {
if (properties == null) {
synchronized (PropertyUtil.class) {
if (properties == null) {
if (path == null) {
path = System.getProperty("user.dir") + File.separator;
}
filePath = path.endsWith("/") || path.endsWith("\\") ? path + "config.properties" : path;
File file = new File(filePath);
if (!file.exists()) {
try {
if (!file.createNewFile()) {
log.error("Creating {} failed.", filePath);
return;
}
} catch (IOException e) {
log.error("Error when creating {}: {}", filePath, e.getMessage());
return;
}
}
try (FileInputStream fis = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
properties = new Properties();
properties.load(inputStreamReader);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 获取值
*
* @param key
* @return java.lang.String
* @author XanderYe
* @date 2020-03-15
*/
public static String get(String key) {
if (properties == null) {
return null;
}
return properties.getProperty(key);
}
/**
* 获取值,为空返回默认值
*
* @param key
* @param defaultValue
* @return java.lang.String
* @author XanderYe
* @date 2022/1/21
*/
public static String get(String key, String defaultValue) {
if (properties == null) {
return defaultValue;
}
return Optional.ofNullable(properties.getProperty(key)).orElse(defaultValue);
}
/**
* 重写保存方法,不转义
*
* @param key
* @param value
* @return void
* @author XanderYe
* @date 2020-03-15
*/
public static void save(String key, String value) {
save(key, value, null);
}
/**
* 重写保存方法,不转义
*
* @param key
* @param value
* @param comment
* @return void
* @author XanderYe
* @date 2020-03-15
*/
public static synchronized void save(String key, String value, String comment) {
if (properties != null) {
if (properties.getProperty(key) == null) {
append(key, value, comment);
} else {
rewrite(key, value, comment);
}
properties.setProperty(key, value);
}
}
/**
* 追加文件
*
* @param key
* @param value
* @param comment
* @return void
* @author XanderYe
* @date 2020-03-15
*/
private static void append(String key, String value, String comment) {
try (FileOutputStream fos = new FileOutputStream(filePath, true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
if (comment != null) {
bw.write("#" + comment);
bw.newLine();
}
bw.write(key + "=" + value);
bw.newLine();
bw.flush();
} catch (IOException e) {
log.error("Error when appending key {}: {}", key, e.getMessage());
}
}
/**
* 重写文件
*
* @param key
* @param value
* @param comment
* @return void
* @author XanderYe
* @date 2020-03-15
*/
private static void rewrite(String key, String value, String comment) {
try (FileOutputStream fos = new FileOutputStream(filePath);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
Enumeration<?> enumeration = properties.keys();
while (enumeration.hasMoreElements()) {
String k = (String) enumeration.nextElement();
String val = properties.getProperty(k);
if (k.equals(key)) {
if (comment != null) {
bw.write("#" + comment);
bw.newLine();
}
bw.write(key + "=" + value);
} else {
bw.write(k + "=" + val);
}
bw.newLine();
}
bw.flush();
} catch (IOException e) {
log.error("Error when rewriting key {}: {}", key, e.getMessage());
}
}
}