MixAll.java

import java.lang.reflect.Method;
import java.util.Properties; public class MixAll { /**
* 将Properties中的值写入Object
*/
public static void properties2Object(final Properties p, final Object object) {
Method[] methods = object.getClass().getMethods();
for (Method method : methods) {
String mn = method.getName();
if (mn.startsWith("set")) {
try {
String tmp = mn.substring(4);
String first = mn.substring(3, 4); String key = first.toLowerCase() + tmp;
String property = p.getProperty(key);
if (property != null) {
Class<?>[] pt = method.getParameterTypes();
if (pt != null && pt.length > 0) {
String cn = pt[0].getSimpleName();
Object arg = null;
if (cn.equals("int")) {
arg = Integer.parseInt(property);
} else if (cn.equals("long")) {
arg = Long.parseLong(property);
} else if (cn.equals("double")) {
arg = Double.parseDouble(property);
} else if (cn.equals("boolean")) {
arg = Boolean.parseBoolean(property);
} else if (cn.equals("String")) {
arg = property;
} else {
continue;
}
method.invoke(object, new Object[] { arg });
}
}
} catch (Throwable e) {
}
}
}
} }

PersonConfig.java

public class PersonConfig {

    private String name;
private int age;
private double saving; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getSaving() {
return saving;
} public void setSaving(double saving) {
this.saving = saving;
} @Override
public String toString() {
return "PersonConfig [name=" + name + ", age=" + age + ", saving="
+ saving + "]";
}
}

TestCommandline.java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; public class TestCommandline {
public static void main(String[] args) throws ParseException, IOException {
Options options = new Options();
Option opt = new Option("c", "config", true, "config file path");
opt.setRequired(true);
options.addOption(opt); CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args); String optNmae = "c";
if (commandLine.hasOption(optNmae)) {
File file = new File(commandLine.getOptionValue(optNmae)); InputStream in = new BufferedInputStream(new FileInputStream(file));
Properties properties = new Properties();
properties.load(in); PersonConfig personConfig = new PersonConfig();
System.out.println(personConfig); // 前
MixAll.properties2Object(properties, personConfig);
System.out.println(personConfig); // 后 System.out.println("load config properties file OK, " + file);
in.close();
}
}
}

E:\config\person.properties

name=Zno
age=18
saving=200.0

执行参数:

-c E:/config/person.properties

运行结果:

PersonConfig [name=null, age=, saving=0.0]
PersonConfig [name=Zno, age=, saving=200.0]
load config properties file OK, E:\config\person.properties

动态修改属性

     import org.apache.commons.beanutils.BeanUtils;

        public void clean() {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
try {
String name = field.getName();
String property = BeanUtils.getProperty(this, name);
if ("未知".equals(property)) {
BeanUtils.setProperty(this, name, null);
}
} catch (Exception e) {
}
}
}

最新文章

  1. 【Python五篇慢慢弹】快速上手学python
  2. Java基础之类Class使用
  3. [工作中的设计模式]组合模式compnent
  4. HTML学习体会
  5. .net发邮件【转】
  6. “HTTPS”安全在哪里?
  7. logstash学习2
  8. PC上安装多个操作系统
  9. HDU5727 Necklace
  10. windows环境下mysql忘记密码如何重置
  11. Red5 配置RTMPT
  12. 2016021801 - Java内存区域学习笔记
  13. $(document).ready(function(){});不执行
  14. HDU 2136 Largest prime factor
  15. Dubbo原理解析-Dubbo内核实现之SPI简单介绍
  16. DB2 的代理 (agent)
  17. Microsoft Office Access数据库或项目包含一个对文件“dao360.dll”版本5.0.的丢失的或损坏的引用。
  18. 使用 php 7.3.0 官网的压缩包安装 FastAdmin
  19. 遍历 SortedList&lt;string, string&gt; 中的值(可用于datatable转json)
  20. Docker入门与应用系列(三)容器管理

热门文章

  1. JS对象基础
  2. uva580Critical Mass
  3. Self-Paced Training (3) - Docker Operations
  4. 【Unity3D】Unity自带组件—完成第一人称人物控制
  5. 【转】超全!整理常用的iOS第三方资源 -- 不错
  6. 【c++内存分布系列】单独一个类
  7. IIS部署网站
  8. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题
  9. codeforces 678E Another Sith Tournament 概率dp
  10. Apache OFBiz 学习笔记 之 服务引擎 一