properties文件操作(读写)工具类


import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Set;

/**
 * @author lidg
 * @date 2022/6/27
 * @desc **.properties文件操作工具类
 */
public enum PropertiesUtil {
    /**
     * 实例
     */
    INSTANCE;

    /**
     * application文件路径
     */
    private String applicationFilePath = "application.properties";

    /**
     * 配置文件Properties
     */
    private final Properties application = new Properties();

    PropertiesUtil() {
        init();
    }

    private PropertiesUtil init() {
        // 获取配置文件绝对路径
        String appFilePath = AppPathUtil.INSTANCE.getSourceAbsolutPath(applicationFilePath);
        try (FileInputStream fileInputStream = new FileInputStream(appFilePath)) {
            application.load(fileInputStream);
        } catch (Exception e) {
            // 部署配置文件加载错误
            throw new RuntimeException("properties.load.error", e);
        }
        return this;
    }

    /**
     * 配置要读取的配置文件相对于resource的路径
     *
     * @param propertiesPath 配置文件路径,没有值默认为resource下的application.properties
     *
     * @return
     */
    public PropertiesUtil setPropertiesPath(String propertiesPath) {
        if (StringUtils.isNotBlank(propertiesPath)) {
            applicationFilePath = propertiesPath;
        }
        return init();
    }

    /**
     * 新增配置文件属性
     *
     * @param key   键
     * @param value 值
     */
    public void addProperty(String key, String value) {
        application.setProperty(key, value);
    }

    /**
     * 设置配置文件属性
     *
     * @param key   键
     * @param value 值
     *
     * @throws Exception
     */
    public void setProperty(String key, String value) throws Exception {
        propertyExists(key);
        application.setProperty(key, value);
    }

    /**
     * 获取配置文件属性
     *
     * @param key 键
     *
     * @return
     *
     * @throws Exception
     */
    public String getProperty(String key) throws Exception {
        propertyExists(key);
        return application.getProperty(key);
    }

    /**
     * 判断键是否存在于配置文件中
     *
     * @param key 键
     *
     * @throws Exception
     */
    private void propertyExists(String key) throws Exception {
        Set<String> names = application.stringPropertyNames();
        if (!names.contains(key)) {
            //配置文件中不存在此属性
            throw new Exception();
        }
    }

    /**
     *  刷新properties文件
     * @throws Exception
     */
    public void updateProperties() throws Exception {
        String appFilePath = AppPathUtil.INSTANCE.getSourceAbsolutPath(applicationFilePath);
        try (FileOutputStream outputStream = new FileOutputStream(appFilePath)) {
            application.store(outputStream, null);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

}

获取路径的工具类: AppPathUtil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值