forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropUtil.java
More file actions
108 lines (96 loc) · 2.49 KB
/
PropUtil.java
File metadata and controls
108 lines (96 loc) · 2.49 KB
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
package org.tron.common.utils;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PropUtil {
public static String readProperty(String file, String key) {
InputStream is = null;
FileInputStream fis = null;
Properties prop;
try {
prop = new Properties();
fis = new FileInputStream(file);
is = new BufferedInputStream(fis);
prop.load(is);
String value = new String(prop.getProperty(key, "").getBytes("ISO-8859-1"), "UTF-8");
return value;
} catch (Exception e) {
logger.error("{}", e);
return "";
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
logger.warn("{}", e);
}
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
logger.error("{}", e);
}
}
}
public static boolean writeProperty(String file, String key, String value) {
FileInputStream in = null;
OutputStream out = null;
BufferedReader br = null;
BufferedWriter bw = null;
Properties properties = new Properties();
try {
in = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(in, UTF_8));
properties.load(br);
out = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(out, UTF_8));
properties.setProperty(key, value);
properties.store(bw, "Generated by the application. PLEASE DO NOT EDIT! ");
return true;
} catch (Exception e) {
logger.warn("{}", e);
return false;
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
logger.warn("{}", e);
}
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
logger.warn("{}", e);
}
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
logger.warn("{}", e);
}
try {
if (bw != null) {
bw.close();
}
} catch (Exception e) {
logger.warn("{}", e);
}
}
}
}