1public class PropsUtil { 2 3 private static final Logger logger = LoggerFactory.getLogger(PropsUtil.class); 4 5 private Properties props = null; 6 7 public PropsUtil(String propsPath) { 8 this(propsPath, "UTF-8"); 9 } 10 11 public PropsUtil(String propsPath, String encoding) { 12 InputStream is = null; 13 try { 14 if (StrUtil.isBlank(propsPath)) { 15 throw new IllegalArgumentException(); 16 } 17 String suffix = ".properties"; 18 if (propsPath.lastIndexOf(suffix) == -1) { 19 propsPath += suffix; 20 } 21 is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsPath); 22 if (is != null) { 23 props = new Properties(); 24 props.load(new InputStreamReader(is, encoding)); 25 } 26 } catch (Exception e) { 27 logger.error("加载属性文件出错!", e); 28 throw new RuntimeException(e); 29 } finally { 30 try { 31 if (is != null) { 32 is.close(); 33 } 34 } catch (IOException e) { 35 logger.error("释放资源出错!", e); 36 } 37 } 38 } 39 40 /** 41 * 加载属性文件,并转为 Map 42 */ 43 public Map<String, String> loadPropsToMap(String propsPath) { 44 Map<String, String> map = new HashMap<String, String>(); 45 for (String key : props.stringPropertyNames()) { 46 map.put(key, props.getProperty(key)); 47 } 48 return map; 49 } 50 51 /** 52 * 获取字符型属性 53 */ 54 public String getString(String key) { 55 return props.getProperty(key); 56 } 57 58 /** 59 * 获取字符型属性,有默认值 60 */ 61 public String getString(String key, String defaultValue) { 62 return props.getProperty(key, defaultValue); 63 } 64 65 /** 66 * 获取字符型属性(有默认值) 67 */ 68 public static String getString(Properties props, String key, String defalutValue) { 69 String value = defalutValue; 70 if (props.containsKey(key)) { 71 value = props.getProperty(key); 72 } 73 return value; 74 } 75 76 /** 77 * 获取数值型属性 78 */ 79 public Integer getInt(String key) { 80 return getInt(key, null); 81 } 82 public Integer getInt(String key, Integer defaultValue) { 83 String value = props.getProperty(key); 84 if (value != null) 85 return Integer.parseInt(value.trim()); 86 return defaultValue; 87 } 88 89 /** 90 * 获取Long型 91 */ 92 public Long getLong(String key) { 93 return getLong(key, null); 94 } 95 public Long getLong(String key, Long defaultValue) { 96 String value = props.getProperty(key); 97 if (value != null) 98 return Long.parseLong(value.trim()); 99 return defaultValue; 100 } 101 102 /** 103 * 获取布尔型属性 104 */ 105 public Boolean getBoolean(String key) { 106 return getBoolean(key, null); 107 } 108 public Boolean getBoolean(String key, Boolean defaultValue) { 109 String value = props.getProperty(key); 110 if (value != null) { 111 value = value.toLowerCase().trim(); 112 if ("true".equals(value)) 113 return true; 114 else if ("false".equals(value)) 115 return false; 116 throw new RuntimeException("The value can not parse to Boolean : " + value); 117 } 118 return defaultValue; 119 } 120 121 public boolean containsKey(String key) { 122 return props.containsKey(key); 123 } 124 125 public Properties getProperties() { 126 return props; 127 } 128 129}
Properties属性文件操作工具类PropsUtil
Stella981
2021-10-11
1025 0 0
点赞
收藏
评论区
加载中...