獲取WEB-INF/classes目錄下配置文件的Java類
通常情況下我們將配置文件放在WEB-INF/classes,如何獲取這個路徑有很多種途徑,下面這個類是我通常使用的類,基本上覆蓋了所有的文件讀取流。 1
import java.io.*;
2
import java.net.*;
3
import java.util.*;
4
5
/** A class to simplify access to resources through the classloader.
6
* @author lavender
7
*/
8
public class Resources extends Object {
9
10
/** Returns the URL of the resource on the classpath
11
* @param resource The resource to find
12
* @throws IOException If the resource cannot be found or read
13
* @return The resource
14
*/
15
public static URL getResourceURL(String resource) throws IOException {
16
URL url = null;
17
ClassLoader loader = Resources.class.getClassLoader();
18
if (loader != null) url = loader.getResource(resource);
19
if (url == null) url = ClassLoader.getSystemResource(resource);
20
if (url == null) throw new IOException("Could not find resource " + resource);
21
return url;
22
}
23
24
/** Returns the URL of the resource on the classpath
25
* @param loader The classloader used to load the resource
26
* @param resource The resource to find
27
* @throws IOException If the resource cannot be found or read
28
* @return The resource
29
*/
30
public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
31
URL url = null;
32
if (loader != null) url = loader.getResource(resource);
33
if (url == null) url = ClassLoader.getSystemResource(resource);
34
if (url == null) throw new IOException("Could not find resource " + resource);
35
return url;
36
}
37
38
/** Returns a resource on the classpath as a Stream object
39
* @param resource The resource to find
40
* @throws IOException If the resource cannot be found or read
41
* @return The resource
42
*/
43
public static InputStream getResourceAsStream(String resource) throws IOException {
44
InputStream in = null;
45
ClassLoader loader = Resources.class.getClassLoader();
46
if (loader != null) in = loader.getResourceAsStream(resource);
47
if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
48
if (in == null) throw new IOException("Could not find resource " + resource);
49
return in;
50
}
51
52
/** Returns a resource on the classpath as a Stream object
53
* @param loader The classloader used to load the resource
54
* @param resource The resource to find
55
* @throws IOException If the resource cannot be found or read
56
* @return The resource
57
*/
58
public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
59
InputStream in = null;
60
if (loader != null) in = loader.getResourceAsStream(resource);
61
if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
62
if (in == null) throw new IOException("Could not find resource " + resource);
63
return in;
64
}
65
66
/** Returns a resource on the classpath as a Properties object
67
* @param resource The resource to find
68
* @throws IOException If the resource cannot be found or read
69
* @return The resource
70
*/
71
public static Properties getResourceAsProperties(String resource)
72
throws IOException {
73
Properties props = new Properties();
74
InputStream in = null;
75
String propfile = resource;
76
in = getResourceAsStream(propfile);
77
props.load(in);
78
in.close();
79
return props;
80
}
81
82
/** Returns a resource on the classpath as a Properties object
83
* @param loader The classloader used to load the resource
84
* @param resource The resource to find
85
* @throws IOException If the resource cannot be found or read
86
* @return The resource
87
*/
88
public static Properties getResourceAsProperties(ClassLoader loader, String resource)
89
throws IOException {
90
Properties props = new Properties();
91
InputStream in = null;
92
String propfile = resource;
93
in = getResourceAsStream(loader, propfile);
94
props.load(in);
95
in.close();
96
return props;
97
}
98
99
/** Returns a resource on the classpath as a Reader object
100
* @param resource The resource to find
101
* @throws IOException If the resource cannot be found or read
102
* @return The resource
103
*/
104
public static InputStreamReader getResourceAsReader(String resource) throws IOException {
105
return new InputStreamReader(getResourceAsStream(resource));
106
}
107
108
/** Returns a resource on the classpath as a Reader object
109
* @param loader The classloader used to load the resource
110
* @param resource The resource to find
111
* @throws IOException If the resource cannot be found or read
112
* @return The resource
113
*/
114
public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
115
return new InputStreamReader(getResourceAsStream(loader, resource));
116
}
117
118
/** Returns a resource on the classpath as a File object
119
* @param resource The resource to find
120
* @throws IOException If the resource cannot be found or read
121
* @return The resource
122
*/
123
public static File getResourceAsFile(String resource) throws IOException {
124
return new File(getResourceURL(resource).getFile());
125
}
126
127
/** Returns a resource on the classpath as a File object
128
* @param loader The classloader used to load the resource
129
* @param resource The resource to find
130
* @throws IOException If the resource cannot be found or read
131
* @return The resource
132
*/
133
public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
134
return new File(getResourceURL(loader, resource).getFile());
135
}
136
137
}

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

132

133

134

135

136

137

posted on 2008-08-19 10:10 iamlavender 閱讀(2602) 評論(0) 編輯 收藏