zoukankan      html  css  js  c++  java
  • properties的公共处理类

    public class PropertiesUtil {
    // 是否是文件
    public static boolean isFile = false;
    // 路径
    public static String path;
    // 单列properties
    private static Properties properties = null;
    // 构造方法初始化文件
    public PropertiesUtil(String path) {
    this.path = path;
    File file = new File(path);
    isFile = file.isFile();
    // TODO Auto-generated constructor stub
    }
    public boolean isFile(String path){
    return isFile;
    };
    // 把配置文件转化为对象
    public Object propertiesToObject(Object object,String path) throws Exception, NoSuchMethodException{
    if(!isFile(path)){
    return null;
    }
    Field[] files = object.getClass().getDeclaredFields();
    Properties properties = load(path);
    for(Field field:files){
    String fieldName = field.getName();
    Class type = field.getType();
    String methodFieldName = "set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
    Method method = object.getClass().getMethod(methodFieldName, type);
    method.invoke(object, properties.get(fieldName));
    }
    return object;
    }
    // 获取配置文件中所有的键值
    public List<Object> getListKey(String path) throws IOException{
    if(!isFile(path)){
    return null;
    }
    Properties properties = load(path);
    Set<Object> set = properties.keySet();
    List<Object>list = new ArrayList<Object>(set);
    return list;
    }
    // 获取配置文件中所有的值
    public List<Object> getListValue(String path) throws IOException{
    if(!isFile(path)){
    return null;
    }
    Properties properties = load(path);
    List<Object> list = new ArrayList<Object>();
    for(Object key:properties.keySet()){
    list.add(properties.get(key));
    }
    return list;
    }
    // 配置文件转成map集合
    public Map<String,Object> getMapKeyValue(String path) throws IOException{
    if(!isFile(path)){
    return null;
    }
    Map<String,Object> resultMap = new HashMap<String,Object>();
    Properties properties = load(path);
    for(Object key : getListKey(path)){
    resultMap.put((String)key, properties.get(key));
    }
    return resultMap;
    }
    public Properties load(String path) throws IOException{
    if(properties == null){
    InputStream stream = new FileInputStream(path);
    properties = new Properties();
    properties.load(stream);
    }
    return properties;
    }
    public static void main(String[]args) throws NoSuchMethodException, Exception{
    String path = "D:/ceshiproperties/admessage.properties";
    PropertiesUtil util = new PropertiesUtil(path);
    System.out.println("此文件是否是一个文件"+util.isFile(path));
    List<Object> listKey = util.getListKey(path);
    for(Object object:listKey){
    System.out.println("配置文件中所有的key值"+object.toString());
    }
    List<Object> listValue=util.getListValue(path);
    for(Object object:listValue){
    System.out.println("配置文件中所有的value"+object.toString());
    }
    AdMessage adMessage = new AdMessage();
    util.propertiesToObject(adMessage, path);
    System.out.println("值1"+adMessage.getMessage_content_changestatus_());
    System.out.println("值2"+adMessage.getMessage_content_delete_());
    System.out.println(util.getMapKeyValue(path));
    System.exit(0);
    }
    }

  • 相关阅读:
    问题解决:访问自己搭建网页时出现:此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。
    cracer教程5----漏洞分析(下)
    cracer教程3----信息收集
    linux3
    pwdump7的用法及其hash值解密
    maven scope含义的说明
    Spark2.0协同过滤与ALS算法介绍
    Jmeter压力测试工具安装及使用教程
    过滤器(Filter)与拦截器(Interceptor )区别
    @Value()读取配置文件属性,读出值为null的问题
  • 原文地址:https://www.cnblogs.com/bingru/p/7527634.html
Copyright © 2011-2022 走看看