Properties属性文件操作工具类PropsUtil

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}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )