配置文件 app.properties 截取如下:
1
ftp.username=userOne
2
ftp.password=p@ssword
3
ftp.hostip=127.0.0.1
4
ftp.port=21

2

3

4

加載并讀取配置
1
import org.apache.commons.configuration.ConfigurationException;
2
import org.apache.commons.configuration.PropertiesConfiguration;
3
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
4
5

6
7
private static String ftpUser = null;
8
9
private static String ftpPassword = null;
10
11
private static String ftpHost = null;
12
13
private static String ftpPort = null;
14
private static String CONFIG_FILEPATH = ClassLoader.getSystemResource(
"app.properties").getPath();
15

16
17
private static void initFromProperties()
18
{
19
try
20
{
21
CONFIG_FILEPATH = URLDecoder.decode(CONFIG_FILEPATH, "utf-8");
22
setProperties(new PropertiesConfiguration(CONFIG_FILEPATH));
23
getProperties().setReloadingStrategy(
24
new FileChangedReloadingStrategy());
25
getProperties().setAutoSave(true);
26
27
readValues();
28
}
29
catch (UnsupportedEncodingException e)
30
{
31
//處理異常
32
}
33
catch (ConfigurationException e)
34
{
35
//處理異常
}
36
37
}
38
39

40
41
private static void readValues()
42
{
43
// ftp
44
setFtpUser(getStrValue("ftp.username"));
45
setFtpPassword(getStrValue("ftp.password"));
46
setFtpHost(getStrValue("ftp.hostip"));
47
setFtpRootPath(getStrValue("ftp.ftproot"));
48
setFtpPort(getStrValue("ftp.port"));
49
}
50
51

52
53

2

3

4

5


6

7

8

9

10

11

12

13

14
private static String CONFIG_FILEPATH = ClassLoader.getSystemResource(
"app.properties").getPath();

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

同樣對于XML配置文件,也可以使用apache commons包.