JavaProperties文件操作

JavaProperties文件操作,各种操作,分享一下

package cn.cpou.modScore.web;

import org.junit.Test;

import java.io.*;
import java.util.*;

/**
 * <code>property文件控制类</code>
 *
 * @author YuLong
 */
@SuppressWarnings("all")
public class PropertiesManager {
    public static String resourceName = "webService.properties";

    public static String getResourceName() {
        return resourceName;
    }

    public static void setResourceName(String resourceName) {
        PropertiesManager.resourceName = resourceName;
    }

    /**
     * 通过key读取property配置
     *
     * @param key
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static String getPropertyValue(String key) throws Exception {

        return getPropertyValue(resourceName, key);
    }

    /**
     * 通过配置文件路径名称以及key读取value
     *
     * @param resourcePath
     * @param key
     * @return
     * @throws Exception
     * @date 2019-7-21 上午11:17:48
     * @author YuLong
     */
    public static String getPropertyValue(String resourcePath, String key) throws Exception {
        String path = resourcePath;
        if (path == null || "".equals(path)) {
            path = resourceName;
        }
        Properties pr = getProperties(path);
        return getPropertyValue(pr, key);
    }

    @Test
    public void test1() {
        try {
            String val = PropertiesManager.getPropertyValue("basePush.properties", "wbFromId");
            System.out.println("修改前" + val);

            PropertiesManager.updatePropertyValue("basePush.properties", "wbFromId", "23");

            val = PropertiesManager.getPropertyValue("basePush.properties", "wbFromId");
            System.out.println("修改后" + val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updatePropertyValue(String filePath, String parameterName, String parameterValue) throws Exception {
        Properties prop = new Properties();
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = PropertiesManager.class.getResourceAsStream("/" + filePath);
            // InputStream fis = new FileInputStream(filePath);
            prop.load(fis);
            fis.close();
            fos = new FileOutputStream(new File(filePath));
            prop.setProperty(parameterName, parameterValue);
            prop.store(fos, "Update '" + parameterName + "' value");
            fos.flush();
        } catch (IOException e) {
            System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");
        } finally {
            if (null != fis)
                fis.close();
            if (null != fos)
                fos.close();
        }

    }

    /**
     * 通过Properties和key读取Propertis文件value
     *
     * @param pr
     * @param key
     * @return
     * @throws Exception
     * @date 2019-7-21 上午11:17:03
     * @author YuLong
     */
    public static String getPropertyValue(Properties pr, String key) throws Exception {
        if (!propertyKeyIsExist(pr, key)) {
            return "";
        }
        return pr.getProperty(key);
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     * @throws Exception
     * @date 2019-7-21 上午11:30:29
     * @author YuLong
     */
    public static boolean propertyKeyIsExist(String key) throws Exception {
        return propertyKeyIsExist(resourceName, key);

    }

    /**
     * 判断key是否存在
     *
     * @param resourcePath
     * @param key
     * @return
     * @throws Exception
     * @date 2019-7-21 上午11:29:32
     * @author YuLong
     */
    public static boolean propertyKeyIsExist(String resourcePath, String key) throws Exception {
        String path = resourcePath;
        if (path == null || "".equals(path)) {
            path = resourceName;
        }
        Properties pr = getProperties(resourceName);
        return propertyKeyIsExist(pr, key);

    }

    /**
     * 判断key是否存在
     *
     * @param pr
     * @param key
     * @return
     * @date 2019-7-21 上午11:26:50
     * @author YuLong
     */
    public static boolean propertyKeyIsExist(Properties pr, String key) {
        boolean flag = false;
        List<String> list = getPropertiesKey(pr);
        if (list == null || list.size() < 1) {
            return flag;
        }
        for (String string : list) {
            if (string.equals(key)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 通过资源路径获取配置文件中 所有key的名称
     *
     * @param resourcePath
     * @return
     * @throws Exception
     * @date 2019-7-21 上午11:27:45
     * @author YuLong
     */
    public static List<String> getPropertiesKey(String resourcePath) throws Exception {
        return getPropertiesKey(getProperties(resourcePath));
    }

    /**
     * 同properties读取所有key的名称
     *
     * @param pr
     * @return
     * @date 2019-7-21 上午11:28:28
     * @author YuLong
     */

    public static List<String> getPropertiesKey(Properties pr) {
        Enumeration e = pr.propertyNames();
        if (!e.hasMoreElements()) {
            System.out.println("资源文件为空!");
            return null;

        }
        List<String> list = new ArrayList<String>();
        while (e.hasMoreElements()) {
            String keyName = (String) e.nextElement();
            list.add(keyName);
        }
        return list;
    }

    /**
     * 根据资源路径读取配置文件
     *
     * @param path
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @author YuLong
     */
    public static Properties getProperties(String path) throws FileNotFoundException, IOException {
        Properties props = null;
        InputStream in = PropertiesManager.class.getResourceAsStream("/" + path);
        if (in != null) {
            props = new Properties();
            props.load(in);
        } else {
            System.out.println("文件不存在");
        }
        return props;
    }

    public static void writeProperties(Map<String, String> map) {
        Properties prop = new Properties();
        InputStream fis = null;
        OutputStream fos = null;
        String realPath = PropertiesManager.class.getClassLoader().getResource("/").getPath();
        System.out.println(realPath);
        try {
            fis = new FileInputStream(realPath + resourceName);
            prop.load(fis);
            fos = new FileOutputStream(realPath + resourceName);
            Set set = map.entrySet();
            Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
            for (int i = 0; i < entries.length; i++) {
                String key = (String) entries[i].getKey();
                String value = (String) entries[i].getValue();
                prop.setProperty(key, value);
                prop.store(fos, "Update'" + value + "'value");
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("发生错误");
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值