java常用API-Properties类

一.概述:
        在实际的项目中大多数的数据都是存放在数据库中的,但是有一些用于配置的信息大多存放在以properties文件中,比如我们常用的log4j.properties文件就是用来配置日志信息的,db.propeties文件就是用来配置数据库信息的。除了一些框架自带的properties文件之外根据项目的需要往往还要自定义properties文件存放一些跟业务相关的配置,比如文件上传路径,某个接口的调用地址等等。为了读写方便java语言提供了java.util.Properties类来操作.properties文件。
二.编程示例:
1.properties文件格式
  properties文件中存放的内容全部为键值对(key=value)开头加#号表示注释,下面是一个简单properties配置文件
  application.properties
 
   
  1. #this is a property file
  2. #database
  3. db.username=jwang
  4. db.password=jwang
  5. #file upload path
  6. file.upload.video=/home/jwang/video
  7. file.upload.image=/home/jwang/image
2.properties文件读写工具类
(1)PorpertiesUtil.java
 
    
  1. package com.jwang.common.utils;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.Properties;
  8. import org.apache.log4j.Logger;
  9. /**
  10. * @author jwang
  11. * properties文件读写工具类
  12. *
  13. */
  14. public abstract class PropertiesUtil
  15. {
  16. private static final Logger LOG = Logger.getLogger(PropertiesUtil.class);
  17. public static Properties getProperties(String path)
  18. {
  19. Properties prop = new Properties();
  20. loadProp(prop, path);
  21. return prop;
  22. }
  23. private static void loadProp(Properties p, String conf)
  24. {
  25. InputStream is = getInputStream(conf);
  26. if(null != is)
  27. {
  28. try
  29. {
  30. p.load(is);
  31. }
  32. catch (IOException e)
  33. {
  34. LOG.info("file not found!");
  35. }
  36. finally
  37. {
  38. if(is != null)
  39. {
  40. try
  41. {
  42. is.close();
  43. }
  44. catch (IOException e)
  45. {
  46. LOG.info("stream close fail!");
  47. }
  48. }
  49. }
  50. }
  51. }
  52. //获取输入流
  53. private static InputStream getInputStream(String conf)
  54. {
  55. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  56. InputStream is = null;
  57. if(null != classLoader)
  58. {
  59. is = classLoader.getResourceAsStream(conf);
  60. }
  61. return is;
  62. }
  63. //获取输出流
  64. private static OutputStream getOutPutStream(String conf)
  65. {
  66. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  67. OutputStream out = null;
  68. if(null != classLoader)
  69. {
  70. String filePath = classLoader.getResource(conf).getFile();
  71. try
  72. {
  73. out = new FileOutputStream(filePath);
  74. }
  75. catch (FileNotFoundException e)
  76. {
  77. LOG.info("file not found!!!");
  78. }
  79. }
  80. return out;
  81. }
  82. //根据key读取value
  83. public static String getValue(Properties p, String key)
  84. {
  85. String value = p.getProperty(key);
  86. return value == null?"":value;
  87. }
  88. //设置key=value
  89. public static void setValue(String conf, String key, String value)
  90. {
  91. Properties p = getProperties(conf);
  92. OutputStream out = getOutPutStream(conf);
  93. p.setProperty(key, value);
  94. try
  95. {
  96. p.store(out, "set:"+key+"="+value);
  97. }
  98. catch (IOException e)
  99. {
  100. LOG.info("set properties fail!!!");
  101. }
  102. finally
  103. {
  104. if(out != null)
  105. {
  106. try
  107. {
  108. out.close();
  109. }
  110. catch (IOException e)
  111. {
  112. LOG.info("stream close fail!");
  113. }
  114. }
  115. }
  116. }
  117. }
(2)测试类:PropertiesTest.java
 
    
  1. package com.jwang.test;
  2. import java.util.Properties;
  3. import org.apache.log4j.Logger;
  4. import org.junit.Test;
  5. import com.jwang.common.utils.PropertiesUtil;
  6. /**
  7. * @author jwang
  8. * 简单的properties文件读写测试类
  9. *
  10. */
  11. public class PropertiesTest
  12. {
  13. private static final Logger LOG = Logger.getLogger(PropertiesTest.class);
  14. private static final String PROPETIES_PATH = "conf/application.properties";
  15. private static final Properties PROP = PropertiesUtil.getProperties(PROPETIES_PATH);
  16. private static final String UPLOAD_PDF_PATH = "/home/jwang/pdf";
  17. @Test
  18. public void testGetProperties()
  19. {
  20. String userName = PROP.getProperty("db.username");
  21. String passWord = PROP.getProperty("db.password");
  22. LOG.info("userName:"+userName);
  23. LOG.info("passWord:"+passWord);
  24. PropertiesUtil.setValue(PROPETIES_PATH, "file.upload.pdf", UPLOAD_PDF_PATH);
  25. }
  26. }
(3)测试结果
 测试前application.properties文件内容
 
 测试后application.properties文件的内容
 
控制台输出如下所示
 
 三.扩展总结(待补充)
 1.Properties类的其他方法
 2.项目中获取文件路径的方法

猜你喜欢

转载自blog.csdn.net/m0_38045882/article/details/78571509