1
// 從資源文件里讀取值的類,文件后綴不一定要.Properties,只要里面內容如:url=www.cnsec.net
2
可通過key(url)取得值-www.cnsec.net,簡單、強大
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.util.Properties;
8
/**
9
* ReadProperties.java
10
* Description: 讀取操作屬性配置文件
11
* @author li.b
12
* @version 2.0
13
* Jun 26, 2008
14
*/
15
public class ReadProperties {
16
17
/**
18
* Description: 獲取屬性配置文件
19
* @param path 資源文件路徑
20
* @return Properties Object
21
* @throws FileNotFoundException
22
* @throws IOException
23
*/
24
public static Properties getProperties(String path) throws FileNotFoundException, IOException{
25
Properties props = null;
26
File file = new File(path);
27
if(file.exists() && file.isFile()){
28
props = new Properties();
29
props.load(new FileInputStream(file));
30
}else{
31
System.out.println(file.toString() + "不存在!");
32
}
33
return props;
34
}
35
36
/**
37
* Description: 從屬性文件獲取值
38
* @param props Properties Object
39
* @param key
40
* @return 通過key匹配到的value
41
*/
42
public static String getValue(Properties props,String key,String encod){
43
String result = "";
44
String en = "";
45
String localEN = System.getProperty("file.encoding");
46
if(encod !=null && !encod.equals("") ){
47
en = encod;
48
}else{
49
en = localEN;
50
}
51
try {
52
key = new String(key.getBytes(en),"ISO-8859-1");
53
result = props.getProperty(key);
54
if(!result.equals("")){
55
result = new String(result.getBytes("ISO-8859-1"),en);
56
}
57
} catch (Exception e) {
58
}finally{
59
if(result == null)result = "";
60
return result;
61
}
62
}
63
64
public static String getValue(Properties props,String key){
65
return getValue(props, key, "");
66
}
67
68
}

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
