PropertiesUtils.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package cn.feishu.sdk.util;
  2. import java.io.InputStream;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Properties;
  7. /**
  8. * @author jeecg-boot
  9. * 2019年11月12日10:04:00
  10. */
  11. public class PropertiesUtils {
  12. private static Map<String, String> cache = new HashMap<String, String>();
  13. public static String getConfig(String key) {
  14. return getValue("config", key);
  15. }
  16. public static String getValue(String name, String key) {
  17. try {
  18. String cacheValue = (String) cache.get(name + key);
  19. if (cacheValue != null) {
  20. return cacheValue;
  21. }
  22. Properties props = new Properties();
  23. InputStream in = PropertiesUtils.class.getResourceAsStream("/"
  24. + name + ".properties");
  25. props.load(in);
  26. String value = props.getProperty(key);
  27. cache.put(name + key, value);
  28. props.clone();
  29. in.close();
  30. return value;
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return null;
  35. }
  36. public static Map<String, String> getMap(String name) {
  37. try {
  38. Properties props = new Properties();
  39. InputStream in = PropertiesUtils.class.getResourceAsStream("/"
  40. + name + ".properties");
  41. props.load(in);
  42. Iterator<Object> it = props.keySet().iterator();
  43. Map<String, String> map = new HashMap<String, String>();
  44. while (it.hasNext()) {
  45. String key = it.next().toString();
  46. String value = props.getProperty(key);
  47. map.put(key, value);
  48. }
  49. props.clone();
  50. in.close();
  51. return map;
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. return null;
  56. }
  57. }