首先我在src目錄下創建了一個test.propertise的文件:



讀取該文件的用例代碼如下:
1
import java.io.BufferedInputStream;
2
import java.io.FileInputStream;
3
import java.io.FileNotFoundException;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Locale;
7
import java.util.Properties;
8
import java.util.PropertyResourceBundle;
9
import java.util.ResourceBundle;
10
11
public class PropertiesReader {
12
/**
13
* 1.使用java.util.Properties類的load()方法
14
*/
15
public static void method1(){
16
try {
17
InputStream in = new BufferedInputStream(new FileInputStream("D:/joneworkspace/joneworld/src/test.properties"));
18
Properties p = new Properties();
19
p.load(in);
20
System.out.println("name is "+p.getProperty("name"));
21
System.out.println("age is "+p.getProperty("age"));
22
System.out.println("score is "+p.getProperty("score"));
23
} catch (FileNotFoundException e) {
24
e.printStackTrace();
25
} catch (IOException e) {
26
e.printStackTrace();
27
}
28
}
29
30
/**
31
* 2.使用java.util.ResourceBundle類的getBundle()方法
32
*/
33
public static void method2(){
34
ResourceBundle rb = ResourceBundle.getBundle("test", Locale.getDefault());
35
System.out.println("name is "+rb.getString("name"));
36
System.out.println("age is "+rb.getString("age"));
37
System.out.println("score is "+rb.getString("score"));
38
}
39
40
/**
41
* 3.使用java.util.PropertyResourceBundle類的構造函數
42
*/
43
public static void method3(){
44
try {
45
InputStream in = new BufferedInputStream(new FileInputStream("D:/joneworkspace/joneworld/src/test.properties"));
46
ResourceBundle rb = new PropertyResourceBundle(in);
47
System.out.println("name is "+rb.getString("name"));
48
System.out.println("age is "+rb.getString("age"));
49
System.out.println("score is "+rb.getString("score"));
50
} catch (FileNotFoundException e) {
51
e.printStackTrace();
52
} catch (IOException e) {
53
e.printStackTrace();
54
}
55
}
56
57
/**
58
* 4.使用class變量的getResourceAsStream()方法
59
*/
60
public static void method4(){
61
InputStream in = PropertiesReader.class.getResourceAsStream("test.properties");
62
Properties p = new Properties();
63
try {
64
p.load(in);
65
System.out.println("name is "+p.getProperty("name"));
66
System.out.println("age is "+p.getProperty("age"));
67
System.out.println("score is "+p.getProperty("score"));
68
} catch (IOException e) {
69
e.printStackTrace();
70
}
71
}
72
/**
73
*5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
74
*/
75
public static void method5(){
76
InputStream in = PropertiesReader.class.getClassLoader().getResourceAsStream("test.properties");
77
Properties p = new Properties();
78
try {
79
p.load(in);
80
System.out.println("name is "+p.getProperty("name"));
81
System.out.println("age is "+p.getProperty("age"));
82
System.out.println("score is "+p.getProperty("score"));
83
} catch (IOException e) {
84
e.printStackTrace();
85
}
86
}
87
88
/**
89
* 6.使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
90
*/
91
public static void method6(){
92
InputStream in = ClassLoader.getSystemResourceAsStream("test.properties");
93
Properties p = new Properties();
94
try {
95
p.load(in);
96
System.out.println("name is "+p.getProperty("name"));
97
System.out.println("age is "+p.getProperty("age"));
98
System.out.println("score is "+p.getProperty("score"));
99
} catch (IOException e) {
100
e.printStackTrace();
101
}
102
}
103
/**
104
* Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
105
* 示例: InputStream in = context.getResourceAsStream(path);
106
* Properties p = new Properties();
107
* p.load(in);
108
*/
109
}
110

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
