赵计刚
每天进步一点点
随笔 - 234,  文章 - 0,  评论 - 243,  阅读 - 166万

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。注意:这个工具类是值得借鉴的。

posted on   赵计刚  阅读(1266)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示