java加載properties文件的六種方法總結
java加載properties文件的六中基本方式實現
java加載properties文件的方式主要分為兩大類:一種是通過import java.util.Properties類中的load(InputStream in)方法加載;
另一種是通過import java.util.ResourceBundle類的getBundle(String baseName)方法加載。
注意:一定要區分路徑格式
實現代碼如下:
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
|
package
com.util;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Properties;
import
java.util.PropertyResourceBundle;
import
java.util.ResourceBundle;
public
class
PropertiesUtil {
private
static
String basePath =
"src/prop.properties"
;
private
static
String name =
""
;
private
static
String nickname =
""
;
private
static
String password =
""
;
/**
* 一、 使用java.util.Properties類的load(InputStream in)方法加載properties文件
*
*/
public
static
String getName1() {
try
{
Properties prop =
new
Properties();
InputStream is =
new
FileInputStream(basePath);
prop.load(is);
name = prop.getProperty(
"username"
);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
name;
}
/**
* 二、 使用class變量的getResourceAsStream()方法
* 注意:getResourceAsStream()讀取路徑是與本類的同一包下
*
*/
public
static
String getName2() {
Properties prop =
new
Properties();
InputStream is = PropertiesUtil.
class
.getResourceAsStream(
"/com/util/prop.properties"
);
try
{
prop.load(is);
name = prop.getProperty(
"username"
);
}
catch
(IOException e) {
e.printStackTrace();
}
return
name;
}
/**
* 三、
* 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
* getResourceAsStream(name)方法的參數必須是包路徑+文件名+.后綴 否則會報空指針異常
*
*/
public
static
String getName3() {
Properties prop =
new
Properties();
InputStream is = PropertiesUtil.
class
.getClassLoader()
.getResourceAsStream(
"com/util/prop.properties"
);
try
{
prop.load(is);
}
catch
(IOException e) {
e.printStackTrace();
}
return
name;
}
/**
* 四、 使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
* getSystemResourceAsStream()方法的參數格式也是有固定要求的
*
*/
public
static
String getName4() {
Properties prop =
new
Properties();
InputStream is = ClassLoader
.getSystemResourceAsStream(
"com/util/prop.properties"
);
try
{
prop.load(is);
name = prop.getProperty(
"username"
);
}
catch
(IOException e) {
e.printStackTrace();
}
return
name;
}
/**
* 五、 使用java.util.ResourceBundle類的getBundle()方法
* 注意:這個getBundle()方法的參數只能寫成包路徑+properties文件名,否則將拋異常
*
*/
public
static
String getName5() {
ResourceBundle rb = ResourceBundle.getBundle(
"com/util/prop"
);
password = rb.getString(
"password"
);
return
password;
}
/**
* 六、 使用java.util.PropertyResourceBundle類的構造函數
*
*/
public
static
String getName6() {
try
{
InputStream is =
new
FileInputStream(basePath);
ResourceBundle rb =
new
PropertyResourceBundle(is);
nickname = rb.getString(
"nickname"
);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
nickname;
}
/**
* 測試
*
*/
public
static
void
main(String[] args) {
System.out.println(
"name1:"
+ PropertiesUtil.getName1());
System.out.println(
"name2:"
+ PropertiesUtil.getName2());
System.out.println(
"name3:"
+ PropertiesUtil.getName3());
System.out.println(
"name4:"
+ PropertiesUtil.getName4());
System.out.println(
"password:"
+ PropertiesUtil.getName5());
System.out.println(
"nickname:"
+ PropertiesUtil.getName6());
}
}
|