ITeye博客

Properties读取类

feigme · 2007-12-12 22:44 · Java · Markdown

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

java 代码

 

  1. package cn.feigme.util;
  2. import java.net.URL;
  3. import java.util.*;
  4. /**
  5. * Util class that will read properties from the WEB-INF/classes/directory
  6. * or by specifying a URL on the filesystem.
  7. * Also has a helper method for creating a platform independent URL.
  8. */
  9. 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 <code>Properties<code> 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. }

评论(2)

  1. #2 楼 · caizhongda · 2009-06-06 11:19

    哥们怎么不写一下中文注释啊..

  2. #1 楼 · 阳光雨露 · 2009-02-11 10:34

    还算可以...

评论功能维护中,当前仅展示历史评论。