java解析Properties配置文件为对象Bean
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
利用java的反射解析Properties文件转成对象 /** * 解析properties文件为对象 * @param <T> * @param propPath * @param cls * @return * @throws InstantiationException * @throws IllegalAccessException * @throws IOException */ public static <T> T paseObject(String propPath, Class<T> cls) throws InstantiationException, IllegalAccessException, IOException { Properties prop = getConfigProperties(propPath); T obj = cls.newInstance(); Field[] fList = obj.getClass().getDeclaredFields(); for (Field field : fList) { String value = prop.getProperty(field.getName()); String typeName = field.getType().getTypeName(); System.out.println(value); System.out.println(typeName); if (value != null) { value = value.trim(); if(typeName.equals("java.lang.String")) { field.set(obj, value); }else if(typeName.equals("int")) { field.setInt(obj, Integer.parseInt(value)); }else if(typeName.equals("float")) { field.setFloat(obj, Float.parseFloat(value)); }else if(typeName.equals("double")) { field.setDouble(obj, Double.parseDouble(value)); }else if(typeName.equals("long")) { field.setLong(obj, Long.parseLong(value)); }else if(typeName.equals("boolean")) { field.setBoolean(obj, Boolean.parseBoolean(value)); }else if(typeName.equals("byte")) { field.setByte(obj, Byte.parseByte(value)); }else if(typeName.equals("short")) { field.setShort(obj, Short.parseShort(value)); } } } return obj; }
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xml](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xmind](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 3
- 资源: 21
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)