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


    1、代码实现

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

    1 #每个路由的最大连接数
    2 httpclient.max.conn.per.route = 20   
    3 #最大总连接数
    4 httpclient.max.conn.total = 400
    5 #连接超时时间(ms)
    6 httpclient.max.conn.timeout = 1000
    7 #操作超时时间(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      * 加载属性文件*.properties
    16      * @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));//属性文件中有这个key
    62         System.out.println(FileUtil.getInt(props, "httpclient.max.conn.per.route2", 10));//属性文件中没有这个key
    63     }
    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。注意:这个工具类是值得借鉴的。

  • 相关阅读:
    iOS网络编程开发-数据加密
    iOS网络编程开发GET请求和POST请求
    iOS网络编程开发—HTTP协议
    iOS网络编程开发—网络编程基础
    iOS多线程技术—自定义NSOperation
    iOS多线程技术—NSOperation用法
    大家一起和snailren学java-(一)对象导论
    大家一起和snailren学java-(序)
    Hadoop 2.7.0模拟分布式实验环境搭建[亲测]
    LeetCode:103Binary Tree Zigzag Level Order Traversal
  • 原文地址:https://www.cnblogs.com/java-zhao/p/5098813.html
Copyright © 2020-2023  润新知