properties读写

第一种方式

Properties properties = new Properties();
try {

    properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
    //遍历取值
    Set<Object> objects = properties.keySet();
    for (Object object : objects) {
        //取到参数赋值给静态变量
        if (object.toString().equals("webservice.uri")) {
            BaseUrl = new String(properties.getProperty((String) object));
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

第二种方式工具类

resource下application路径:src/main/resources/properties/test.properties

package com.scy.dispatch.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtils {

    private static Properties ps = new Properties();
    private static String proPath = null;
    private static Map<String, String> proLoadMap = null;

    public PropertiesUtils(String path) {
        // TODO Auto-generated constructor stub
        proPath = path;
        proLoadMap = getPropertiesMap(path);
    }

    public PropertiesUtils() {

    }

    /**
     * 获取文件的values值
     *
     * @param filePath
     * @param key
     * @return
     */
    public static String getPropertiesValue(String filePath, String key) {
        if (null != filePath) {
            String value = null;
            File propertiesFile = new File(filePath);
            if (!propertiesFile.exists()) {
                return null;
            }
            Properties properties = new Properties();
            FileInputStream propertiesInputStream = null;
            try {
                propertiesInputStream = new FileInputStream(propertiesFile);
                properties.load(propertiesInputStream);
                value = properties.getProperty(key).trim();
//          System.err.println(value);
                propertiesInputStream.close();
            } catch (Exception e) {
//           TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (null != propertiesInputStream)
                        propertiesInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (value.isEmpty())
                return null;
            return value;

        } else {
            return null;
        }

    }

    /**
     * 设置properties文件k,v
     *
     * @param filePath
     * @param key
     * @param value
     * @return
     */
    public static boolean setProertiesConf(String filePath, String key, String value) {
        if (null != filePath && null != key && null != value && !key.equals("") && !value.equals("")) {
            FileOutputStream out = null;
            try {
                PropertiesUtils properties = new PropertiesUtils(filePath);
                @SuppressWarnings("static-access")
                Map<String, String> wen = properties.getPropertiesMap(filePath);
                wen.put(key, value);

                for (Map.Entry<String, String> entry : wen.entrySet()) {
                    ps.setProperty(entry.getKey(), entry.getValue());
                }
                File propertiesFile = new File(filePath);
                if (!propertiesFile.exists()) {
                    return false;
                }
                out = new FileOutputStream(propertiesFile);
                ps.store(out, null);
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return false;

            } finally {
                if (null != out) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        } else {
            return false;

        }

    }

    @SuppressWarnings("unused")
    private static boolean mapToProperties(Map<?,?> map) {
        FileOutputStream out = null;
        for (Map.Entry<String, String> entry : proLoadMap.entrySet()) {
            ps.setProperty(entry.getKey(), entry.getValue());
        }
        File propertiesFile = new File(proPath);
        if (!propertiesFile.exists()) {
            return false;
        }
        try {
            out = new FileOutputStream(propertiesFile);
            ps.store(out, null);
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            return false;
        }finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 将properties配置文件转换城map格式
     *
     * @param propertiesFilePath
     * @return Map<String, String>
     */

    @SuppressWarnings("rawtypes")
    public static Map<String, String> getPropertiesMap(String propertiesFilePath) {

        FileInputStream in = null;
        File propertiesFile = new File(propertiesFilePath);
        if (!propertiesFile.exists()) {
            return null;
        }

        try {
            in = new FileInputStream(propertiesFile);
            ps.load(in);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.getMessage();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
        @SuppressWarnings("unchecked")
        Map<String, String> wen = new HashMap<String, String>((Map) ps);

        return wen;
    }

    /**
     * 获取加载好的Properties的Value值
     *
     * @param key
     * @return String
     */
    public  String getDefaultPropertiesValue(String key) {
        if (null != proLoadMap) {
            return proLoadMap.get(key);
        }
        return null;

    }

    /**
     * 获取加载好的Properties的所有Key
     *
     * @return Set<String>
     */
    public Set<String> getDefaultKeys() {
        if (null != proLoadMap) {
            return proLoadMap.keySet();
        }
        return null;
    }

    /**
     * 设置已经加载好文件的K和V值
     *
     * @param key
     * @param value
     * @return boolean true:成功 false:失败
     */
    public  boolean put(String key, String value) {
        boolean TF = setProertiesConf(proPath, key, value);
        proLoadMap = getPropertiesMap(proPath);
        return TF;
    }

    public Properties getDefaultPs() {
        return ps;
    }

    public  String getDefaultProPath() {
        return proPath;
    }

    public  Map<String, String> updateDefaultProPath(String proPath) {
        PropertiesUtils.proPath = proPath;
        return proLoadMap = getPropertiesMap(proPath);
    }

    public  Map<String, String> getDefaultProLoadMap() {
        return proLoadMap;
    }

    public  void setDefaultProLoadMap(Map<String, String> map) {
        proLoadMap = map;
    }

    public  boolean Commit(Map<String, String> map) {
        return mapToProperties(proLoadMap);
    }


}

1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值