PropertiesUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.iamberry.wechat.tools;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.Enumeration;
  8. import java.util.Map;
  9. import java.util.Properties;
  10. import javax.servlet.ServletContext;
  11. import org.apache.commons.collections.map.HashedMap;
  12. import org.springframework.web.context.ContextLoader;
  13. import org.springframework.web.context.WebApplicationContext;
  14. /**
  15. * description: 读写配置文件工具
  16. * @author: 张应真
  17. * @createDate: 2016年6月13日
  18. */
  19. public class PropertiesUtils {
  20. // private static final String PATH = "config.properties";
  21. // private static final String OUTPATH = "/WEB-INF/classes/ResultInfo.properties";
  22. /**
  23. * 根据key读取value
  24. * @param in
  25. * @param key
  26. * @return
  27. */
  28. public static String readValue(InputStream in, String key) {
  29. Properties props = new Properties();
  30. try {
  31. // InputStream in = new BufferedInputStream(new FileInputStream(filePath));
  32. props.load(in);
  33. String value = props.getProperty(key);
  34. System.out.println("key:"+key +"====================="+ "value:"+value);
  35. return value;
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. return null;
  39. }
  40. }
  41. /**
  42. * 读取properties的全部信息
  43. * @param in
  44. * @return
  45. */
  46. @SuppressWarnings("unchecked")
  47. public static Map<String,String>readProperties(InputStream in) {
  48. Properties props = new Properties();
  49. Map<String, String> map=new HashedMap();
  50. try {
  51. // InputStream in = new BufferedInputStream(new FileInputStream(path));
  52. props.load(in);
  53. Enumeration en = props.propertyNames();
  54. while (en.hasMoreElements()) {
  55. String key = (String) en.nextElement();
  56. String Property = props.getProperty(key);
  57. map.put(key, Property);
  58. // System.out.println(key +"-----"+ Property);
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return map;
  64. }
  65. /**
  66. * 写入properties信息
  67. * @param outpath
  68. * @param parameterName
  69. * @param parameterValue
  70. */
  71. public static void writeProperties(String outpath, String parameterName,String parameterValue) {
  72. Properties prop = new Properties();
  73. try {
  74. //获取ResultInfo.properties路径
  75. // WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
  76. // ServletContext servletContext = webApplicationContext.getServletContext();
  77. // InputStream in=servletContext.getResourceAsStream(outpath);
  78. InputStream in =new FileInputStream(outpath);
  79. // 从输入流中读取属性列表(键和元素对)
  80. prop.load(in);
  81. // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
  82. // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
  83. OutputStream fos = new FileOutputStream(outpath);
  84. prop.setProperty(parameterName, parameterValue);
  85. // 以适合使用 load 方法加载到 Properties 表中的格式,
  86. // 将此 Properties 表中的属性列表(键和元素对)写入输出流
  87. prop.store(fos, "Update '" + parameterName + "' value '"+ parameterValue+"'");
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. System.err.println("路径: " + outpath + "----键:"+ parameterName + "----值:");
  91. }
  92. }
  93. // public static void main(String[] args) {
  94. //
  95. // }
  96. }