Java封装读取properties配置文件的工具类【杭州多测师_王sir】

这篇文章介绍了PropertyUtils类,用于处理SpringBoot项目中的配置文件读取、写入以及获取单个配置项功能,使用了Properties和InputStream/OutputStream进行操作。
摘要由CSDN通过智能技术生成
package cn.duoceshi.springbootdemo.utils;

import lombok.extern.slf4j.Slf4j;

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


@Slf4j
public class PropertyUtils {

    public static Map<String, String> getAll(String filePath) {
        Map<String, String> propertyMap = new HashMap<String, String>();
        File file = new File(filePath);
        if (!file.exists()) {
            log.error("配置文件不存在, 路径: " + filePath);
            return propertyMap;
        }

        Properties properties = new Properties();
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(filePath),"UTF-8");
            properties.load(new BufferedReader(reader));
            Set<String> names = properties.stringPropertyNames();
            if (null == names || names.size() == 0)
                return propertyMap;

            for (String name : names) {
                propertyMap.put(name, properties.getProperty(name));
            }
            return propertyMap;
        } catch (Exception ex) {
            log.error("读取配置文件失败");
        }
        return propertyMap;
    }

    public static Map<String, String> getAll(InputStream inputStream) {
        Map<String, String> propertyMap = new HashMap<String, String>();
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            Set<String> names = properties.stringPropertyNames();
            if (null == names || names.size() == 0)
                return propertyMap;

            for (String name : names) {
                propertyMap.put(name, properties.getProperty(name));
            }
            return propertyMap;
        } catch (Exception ex) {
            log.error("读取配置文件失败");
        }
        return propertyMap;
    }

    public static void writeProperties(String filePath, Map<String, String> props) {
        Properties properties = new Properties();
        FileOutputStream output = null;
        File file = new File(filePath);
        if (!file.exists()) {
            log.error("配置文件不存在, 路径: " + filePath);
            return;
        }
        try {
            output = new FileOutputStream(filePath);
            for (Map.Entry<String, String> entry : props.entrySet()){
                properties.setProperty(entry.getKey(), entry.getValue());
            }
            properties.store(output, new Date().toString());// 保存键值对到文件中
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String get(String filePath, String key) {
        File file = new File(filePath);
        if (!file.exists()) {
            log.error("配置文件不存在, 路径: " + filePath);
            return null;
        }
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
            Set<String> names = properties.stringPropertyNames();
            if (null == names || names.size() == 0)
                return null;

            for (String name : names) {
                if (name.trim().equals(name.trim()))
                    return properties.getProperty(name);
            }
            return null;
        } catch (Exception ex) {
            log.error("读取配置文件失败");
            return null;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值