0
点赞
收藏
分享

微信扫一扫

通用Propterties工具类,兼容读jar同级目录的文件。


1.前言。 
  properties文件的写方法比较不好用,折中写了个通用类。 
2.例子。 


1. import
2. import
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12.   
13. /**
14.  * 读取Properties综合类
15.  * 
16.  * @author lijn 2014-02-25
17.  */
18. public class
19. /**
20.      * 得到某一个类的路径
21.      * 
22.      * @param name
23.      * @return
24.      */
25. public static
26. null;  
27. if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {  
28. "/").toString().replace("file:/", "")  
29. "%20", " ");  
30. else
31. "/").toString().replace("file:", "")  
32. "%20", " ");  
33.         }  
34. return
35.     }  
36.   
37. /**
38.      * 读取所有的property
39.      * 
40.      * @param filename
41.      *            properties文件路径
42.      * @return 所有的property的集合(map形式)
43.      */
44. @SuppressWarnings("unchecked")  
45. public static
46. if (null
47. return null;  
48.         }  
49. class) + filename;  
50. new
51. null;  
52. try
53. new BufferedInputStream(new
54.             props.load(in);  
55. new
56.             Enumeration en = props.propertyNames();  
57. while
58.                 String key = (String) en.nextElement();  
59.                 String Property = props.getProperty(key);  
60.                 map.put(key, Property);  
61.             }  
62. return
63. // 关闭资源
64. catch
65.             e.printStackTrace();  
66. catch
67.             e.printStackTrace();  
68. finally
69. try
70.                 in.close();  
71. catch
72. // TODO Auto-generated catch block
73.                 e.printStackTrace();  
74.             }  
75.         }  
76. return null;  
77.     }  
78.   
79. /**
80.      * 获取某个property的值
81.      * 
82.      * @param filename
83.      *            文件名
84.      * @param key
85.      *            property的key
86.      * @return property的value
87.      */
88. public static
89. if (null == filename || null
90. return null;  
91.         }  
92. class) + filename;  
93. new
94. null;  
95. null;  
96. try
97. new BufferedInputStream(new
98.             props.load(in);  
99. new FileOutputStream(new
100.             props.setProperty(key, value);  
101. "update");  
102. // 关闭资源
103. catch
104.             e.printStackTrace();  
105. catch
106.             e.printStackTrace();  
107. finally
108. try
109.                 out.close();  
110.                 in.close();  
111. catch
112. // TODO Auto-generated catch block
113.                 e.printStackTrace();  
114.             }  
115.         }  
116. return null;  
117.     }  
118.   
119. /**
120.      * 获取某个property的值
121.      * 
122.      * @param filename
123.      *            文件名
124.      * @param key
125.      *            property的key
126.      * @return property的value
127.      */
128. public static
129. if (null == filename || null
130. return null;  
131.         }  
132. class) + filename;  
133. new
134. null;  
135. try
136. new BufferedInputStream(new
137.             props.load(in);  
138. return
139. // 关闭资源
140. catch
141.             e.printStackTrace();  
142. catch
143.             e.printStackTrace();  
144. finally
145. try
146.                 in.close();  
147. catch
148. // TODO Auto-generated catch block
149.                 e.printStackTrace();  
150.             }  
151.         }  
152. return null;  
153.     }  
154.   
155. /**
156.      * 需要将config.properies文件放到和本类同级目录下面
157.      * 
158.      * @param filename
159.      * @param key
160.      * @param value
161.      * @return
162.      */
163. public static
164. if (null == filename || null
165. return null;  
166.         }  
167. class) + filename;  
168. new
169. null;  
170. null;  
171. try
172. class.getResourceAsStream("config.properties");  
173. // System.out.println(PropertiesUtil.class.getClassLoader().getResource("config.properties"));
174. new
175.             props.load(b2);  
176.   
177. new
178.             props.setProperty(key, value);  
179. "update");  
180. // 关闭资源
181. catch
182.             e.printStackTrace();  
183. catch
184.             e.printStackTrace();  
185. finally
186. try
187.                 out.close();  
188.                 in.close();  
189. catch
190. // TODO Auto-generated catch block
191.                 e.printStackTrace();  
192.             }  
193.         }  
194. return null;  
195.     }  
196.   
197. /**
198.      * 读写jar外的properties文件
199.      * 
200.      * @param filename
201.      * @param key
202.      * @param value
203.      */
204. public static void
205.             String value) {  
206. if (null == filename || null
207. return;  
208.         }  
209. "java.class.path");  
210. 0, outPathString  
211. "\\") + 1);  
212.   
213.         String filePath = outPathString + filename;  
214. new
215. null;  
216. null;  
217. try
218. new BufferedInputStream(new
219.             props.load(b2);  
220.   
221. new FileOutputStream(new
222.             props.setProperty(key, value);  
223. "update");  
224. // 关闭资源
225. catch
226.             setProp(filename,key,value);  
227. catch
228.             e.printStackTrace();  
229. finally
230. try
231. if(null!=out){  
232.                  out.close();  
233.                 }  
234. if(null!=b2){  
235.                 b2.close();  
236.                 }  
237. catch
238. // TODO Auto-generated catch block
239.                 e.printStackTrace();  
240.             }  
241.         }  
242.   
243.     }  
244. /**
245.      * 以行的形式写ja内的文件
246.      * 
247.      * @param filename
248.      * @param key
249.      * @param value
250.      */
251. public static void
252. if (null == filename || null
253. return;  
254.         }  
255. class) + filename;  
256.       
257. null;  
258. null;  
259. byte[] cont=new byte[4];  
260. "";  
261. try
262. //先读取源文件的内容,再追加内容
263. new BufferedInputStream(new
264. int
265. while(res!=-1){  
266. new
267.                 res=b2.read(cont);  
268.             }  
269. //追加
270.             contentResult+=content;  
271. new FileOutputStream(new
272.             out.write(contentResult.getBytes());  
273. // 关闭资源
274. catch
275. catch
276.             e.printStackTrace();  
277. finally
278. try
279. if(null!=out){  
280.                  out.close();  
281.                 }  
282. if(null!=b2){  
283.                 b2.close();  
284.                 }  
285. catch
286. // TODO Auto-generated catch block
287.                 e.printStackTrace();  
288.             }  
289.         }  
290.   
291.   
292.     }  
293. /**
294.      * 以行的形式写jar外的文件
295.      * 
296.      * @param filename
297.      * @param key
298.      * @param value
299.      */
300. public static void
301. if (null == filename || null
302. return;  
303.         }  
304. "java.class.path");  
305. 0, outPathString  
306. "\\") + 1);  
307.   
308.         String filePath = outPathString + filename;  
309. null;  
310. null;  
311. byte[] cont=new byte[1024];  
312. "";  
313. try
314. //先读取源文件的内容,再追加内容
315. new BufferedInputStream(new
316. int
317. while(res!=-1){  
318. new
319.                 res=b2.read(cont);  
320.             }  
321. //追加
322.             contentResult+=content;  
323. new
324.             out.write(contentResult.getBytes());  
325. // 关闭资源
326. catch
327.             writeLineInerFile(filename,content);  
328. catch
329.             e.printStackTrace();  
330. finally
331. try
332. if(null!=out){  
333.                  out.close();  
334.                 }  
335. if(null!=b2){  
336.                 b2.close();  
337.                 }  
338. catch
339. // TODO Auto-generated catch block
340.                 e.printStackTrace();  
341.             }  
342.         }  
343.   
344.     }  
345. /**
346.      * 读jar外的properties文件的属性
347.      * 
348.      * @param filename
349.      * @param key
350.      * @param value
351.      */
352. public static
353. if (null == filename || null
354. return null;  
355.         }  
356. "java.class.path");  
357. 0, outPathString  
358. "\\") + 1);  
359.   
360.         String filePath = outPathString + filename;  
361. new
362. null;  
363. try
364. new BufferedInputStream(new
365.             props.load(b2);  
366. return
367. // 关闭资源
368. catch
369. return
370. catch
371.             e.printStackTrace();  
372. finally
373. try
374. if(null!=b2){  
375.                     b2.close();  
376.                 }  
377. catch
378. // TODO Auto-generated catch block
379.                 e.printStackTrace();  
380.             }  
381.         }  
382. return null;  
383.   
384.     }  
385.   
386. public static void main(String[] args) throws
387. "error.txt","fiowe");  
388.   
389.     }  
390. }

举报

相关推荐

0 条评论