PropertiesUtils.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package cn.com.ctop.common.utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.InputStream;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Properties;
  9. public class PropertiesUtils {
  10. private static final Logger log = LoggerFactory.getLogger(PropertiesUtils.class);
  11. private static Map<String, String> cache = new HashMap<String, String>();
  12. public static String getConfig(String key) {
  13. return getValue("kuaishou_config", key);
  14. }
  15. public static String getValue(String name, String key) {
  16. try {
  17. String cacheValue = (String) cache.get(name + key);
  18. if (cacheValue != null) {
  19. return cacheValue;
  20. }
  21. Properties props = new Properties();
  22. InputStream in = PropertiesUtils.class.getResourceAsStream("/"
  23. + name + ".properties");
  24. props.load(in);
  25. String value = props.getProperty(key);
  26. cache.put(name + key, value);
  27. props.clone();
  28. in.close();
  29. return value;
  30. } catch (Exception e) {
  31. log.error("PropertiesUtil getCacheValue Error", e);
  32. }
  33. return null;
  34. }
  35. public static Map<String, String> getMap(String name) {
  36. try {
  37. Properties props = new Properties();
  38. InputStream in = PropertiesUtils.class.getResourceAsStream("/"
  39. + name + ".properties");
  40. props.load(in);
  41. Iterator<Object> it = props.keySet().iterator();
  42. Map<String, String> map = new HashMap<String, String>();
  43. while (it.hasNext()) {
  44. String key = it.next().toString();
  45. String value = props.getProperty(key);
  46. map.put(key, value);
  47. }
  48. props.clone();
  49. in.close();
  50. return map;
  51. } catch (Exception e) {
  52. log.error("PropertiesUtil getMap Error", e);
  53. }
  54. return null;
  55. }
  56. }