---
title: "Properties读取类"
author: "feigme"
date: "2007-12-12T22:44:37Z"
tags: []
slug: "properties"
draft: false
lastmod: "2007-12-12T22:44:37Z"
id: 147952
url: "https://feigme.iteye.com/blog/147952"
---

一个properties常用类，以前收藏的

java 代码

 

01. package cn.feigme.util;
02. import java.net.URL;
03. import java.util.\*;
04. /\**
05. * Util class that will read properties from the WEB-INF/classes/directory
06. * or by specifying a URL on the filesystem.
07. * Also has a helper method for creating a platform independent URL.
08. \*/
09. public class PropertyReader {
10. /\**
11. * Retrieve the properties specified by the fileName
12. * The property file should be in the WEB-INF/classess directory
13. * Suppose you need to get the properties in the
14. * web-inf/classes/config/application.properties ,
15. * you need to pass the propertyFile: config/application.properties
16. \*
17. * @param propertyFile relative path to a properties file in the WEB-INF/classes directory
18. * @return a &lt;code&gt;Properties&lt;code&gt; object based on the input file
19. \*\*/
20. public static Properties getProperties(String propertyFile) {
21. try {
22. URL url = getPropertiesURL(propertyFile);
23. return getProperties(url);
24. }
25. catch (Exception e) {
26. System.out.println("Error ocurred during properties retrieval");
27. System.out.println(e.getMessage());
28. return null;
29. }
30. }
31. /\**
32. * This method will return a platform independent URL to a file
33. * in the web-inf/classes direcotry.
34. \*
35. * @param fileName relative path to a properties file in the WEB-INF/classes directory
36. * @return a platform independent URL to the xml file.
37. \*/
38. public static URL getPropertiesURL(String fileName) {
39. try {
40. System.out.println("Getting the properties URL");
41. URL url = null;
42. url = PropertyReader.class.getResource("/" + fileName);
43. String s = url.toString();
44. System.out.println("Filename of the  properties file is: " + s);
45. if (s.indexOf("file://") != -1) {
46. int indexOf = s.indexOf("file://") + 6;
47. String temp = s.substring(0, indexOf);
48. System.out.println("temp = " + temp + " moet zijn file:/");
49. url = new URL(temp + "//" + s.substring(indexOf));
50. System.out.println("The url is now: " + url);
51. }
52. return url;
53. }
54. catch (Exception e) {
55. System.out.println("Error ocurred during properties retrieval");
56. System.out.println(e.getMessage());
57. return null;
58. }
59. }
60. /\**
61. * Retrieve the properties accesible through the specified URL
62. \*
63. * @param url a reference to a properties file
64. * @return a properties file
65. \*\*/
66. public static Properties getProperties(URL url) {
67. try {
68. Properties props = new Properties();
69. // Check for Solaris compatibility.
70. // A // in the file protocol won't be found in Solaris.
71. props.load(url.openStream());
72. System.out.println("Properties have been loaded: " + props);
73. return props;
74. }
75. catch (Exception e) {
76. System.out.println("Error ocurred during properties retrieval");
77. System.out.println(e.getMessage());
78. return null;
79. }
80. }
81. }