星空网 > 软件开发 > Java

第一章 属性文件操作工具类

1、代码实现

给出的属性文件:http.properties(位于类路径下)

第一章 属性文件操作工具类第一章 属性文件操作工具类
1 #每个路由的最大连接数2 httpclient.max.conn.per.route = 20  3 #最大总连接数4 httpclient.max.conn.total = 4005 #连接超时时间(ms)6 httpclient.max.conn.timeout = 10007 #操作超时时间(ms)8 httpclient.max.socket.timeout = 1000

View Code

注意:eclipse默认的属性编辑器不可以显示中文,安装插件(eclipse--propedit_5.3.3)即可。

属性文件操作工具:FileUtil

第一章 属性文件操作工具类第一章 属性文件操作工具类
 1 package com.util; 2  3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Properties; 6  7 import org.apache.commons.lang.math.NumberUtils; 8  9 /**10  * 文件操作工具类11 */12 public class FileUtil {13   14   /**15    * 加载属性文件*.properties16    * @param fileName 不是属性全路径名称,而是相对于类路径的名称17   */18   public static Properties loadProps(String fileName){19     Properties props = null;20     InputStream is = null;21     22     try {23       is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);//获取类路径下的fileName文件,并且转化为输入流24       if(is != null){25         props = new Properties();26         props.load(is);  //加载属性文件27       }28     } catch (Exception e) {29       e.printStackTrace();30     }finally{31       if(is!=null){32         try {33           is.close();34         } catch (IOException e) {35           e.printStackTrace();36         }37       }38     }39     40     return props;41   }42   43   /*44    * 这里只是列出了从属性文件中获取int型数据的方法,获取其他类型的方法相似45   */46   public static int getInt(Properties props, String key, int defaultValue){47     int value = defaultValue;48     49     if(props.containsKey(key)){                //属性文件中是否包含给定键值50       value = NumberUtils.toInt(props.getProperty(key), defaultValue);//从属性文件中取出给定键值的value,并且转换为int型51     }52     53     return value;54   }55   56   /**57    * 测试58   */59   public static void main(String[] args) {60     Properties props = FileUtil.loadProps("http.properties");61     System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route", 10));//属性文件中有这个key62     System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route2", 10));//属性文件中没有这个key63   }64 }

View Code

注意:

  • 从类路径下读取文件的方法Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
  • getInt()方法:使用了org.apache.commons.lang.math.NumberUtils类的toInt(String str,int defaultValue)方法,观察源代码第一章 属性文件操作工具类第一章 属性文件操作工具类
     1 public static int toInt(String str, int defaultValue) { 2     if(str == null) { 3       return defaultValue; 4     } 5     try { 6       return Integer.parseInt(str); 7     } catch (NumberFormatException nfe) { 8       return defaultValue;//toInt("hello", 123) 9     }10   }

    View Code

    执行流程:当传入的str为null时,直接返回defaultValue;否则,使用Integer.parseInt(str)将str转换为int型,如果转换成功(eg.str="123"),直接返回转换后的int型(eg.123),如果转换不成功(eg.str="hello"),直接返回defaultValue。注意:这个工具类是值得借鉴的。




原标题:第一章 属性文件操作工具类

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊热门仓库爆仓+新规将生效,卖家快调整备货计划:https://www.goluckyvip.com/news/9925.html
最强TikTok引流工具 | 紧追初夏爆款风向!:https://www.goluckyvip.com/news/9926.html
全球成长计划 | 寻汇SUNRATE与TikTok Shop印尼店达成合作 :https://www.goluckyvip.com/news/9927.html
利好消息!Coupang官方CGF/CGF LITE运费补贴5月1日上线!:https://www.goluckyvip.com/news/9928.html
事关跨境卖家,国务院办公厅和海关总署相继发布重磅消息!:https://www.goluckyvip.com/news/9929.html
跨境电商物流是什么_跨境电商物流模式:https://www.goluckyvip.com/news/993.html
加拿大本地账户开立是否需要加拿大身份? :https://www.kjdsnews.com/a/1842166.html
加拿大本地账户开立是否需要加拿大身份? :https://www.xlkjsw.com/news/92266.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流