package com.tudicloud.framework.common.core.util.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * 属性工具类,用于加载和读取配置文件中的属性值。
 *
 * @Author 孙达
 * @Date 2025/11/14 14:10
 * @Wechat sundaain
 * @Email 18211102099@163.com
 * @Copyright <a href="https://www.sundablog.com">孙达博客</a>
 */
public class PropUtils {

    private static Logger logger = LoggerFactory.getLogger(PropUtils.class);

    // ---------------------- load prop value ----------------------

    /**
     * 从资源路径加载属性配置文件。
     *
     * @param resourcePath 资源路径(相对于classpath)
     * @return 加载的Properties对象,如果路径为空或加载失败则返回空的Properties对象
     */
    public static Properties loadProp(String resourcePath) {
        Properties prop = new Properties();
        if (resourcePath == null || resourcePath.trim().isEmpty()) {
            return prop;
        }

        try (InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath)) {
            if (input != null) {
                prop.load(new InputStreamReader(input, StandardCharsets.UTF_8));
            }
        } catch (Exception e) {
            logger.error("PropTool loadProp error:", e);
        }
        return prop;
    }

    /**
     * 从文件系统路径加载属性配置文件。
     *
     * @param fileName 文件路径
     * @return 加载的Properties对象,如果路径为空、文件不存在或加载失败则返回空的Properties对象
     */
    public static Properties loadFileProp(String fileName) {
        Properties prop = new Properties();
        if (fileName == null || fileName.trim().isEmpty()) {
            return prop;
        }

        Path path = Paths.get(fileName);
        if (!Files.exists(path)) {
            return prop;
        }

        try (InputStream input = Files.newInputStream(path)) {
            prop.load(new InputStreamReader(input, StandardCharsets.UTF_8));
        } catch (IOException e) {
            logger.error("PropTool loadFileProp error:", e);
        }
        return prop;
    }

    // ---------------------- read prop value ----------------------

    /**
     * 获取字符串类型的属性值。
     *
     * @param prop 配置属性对象
     * @param key  属性键
     * @return 对应的字符串值,若不存在则返回null
     */
    public static String getString(Properties prop, String key) {
        return prop.getProperty(key);
    }

    /**
     * 获取字符串类型的属性值,支持默认值。
     *
     * @param prop         配置属性对象
     * @param key          属性键
     * @param defaultValue 默认值
     * @return 对应的字符串值,若不存在则返回默认值
     */
    public static String getString(Properties prop, String key, String defaultValue) {
        String value = getString(prop, key);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }

    /**
     * 获取整数类型的属性值。
     *
     * @param prop 配置属性对象
     * @param key  属性键
     * @return 对应的整数值
     * @throws NumberFormatException 如果值不是有效的数字格式
     */
    public static int getInt(Properties prop, String key) {
        return Integer.parseInt(getString(prop, key));
    }

    /**
     * 获取布尔类型的属性值。
     *
     * @param prop 配置属性对象
     * @param key  属性键
     * @return 对应的布尔值
     */
    public static boolean getBoolean(Properties prop, String key) {
        return Boolean.valueOf(getString(prop, key));
    }

    /**
     * 获取长整数类型的属性值。
     *
     * @param prop 配置属性对象
     * @param key  属性键
     * @return 对应的长整数值
     * @throws NumberFormatException 如果值不是有效的数字格式
     */
    public static long getLong(Properties prop, String key) {
        return Long.valueOf(getString(prop, key));
    }

    /**
     * 获取双精度浮点数类型的属性值。
     *
     * @param prop 配置属性对象
     * @param key  属性键
     * @return 对应的双精度浮点数值
     * @throws NumberFormatException 如果值不是有效的数字格式
     */
    public static double getDouble(Properties prop, String key) {
        return Double.valueOf(getString(prop, key));
    }

}
最后修改:2025 年 11 月 14 日
如果觉得我的文章对你有用,请随意赞赏