Class Props

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class Props extends Properties
Properties 配置增强,参考 Hutool 的 Props 设计。

支持从 classpath 相对路径或文件加载 .properties,提供类型安全的读取(带默认值)、 转换为 Bean 与 Map 快照。

Since:
1.9.0
Author:
suretool
See Also:
  • Constructor Details

    • Props

      public Props()
      空配置。
    • Props

      public Props(String path)
      从 classpath 相对路径加载。
      Parameters:
      path - classpath 相对路径(如 "config/app.properties")
      Throws:
      IllegalArgumentException - 资源不存在或加载失败
    • Props

      public Props(File file)
      从文件加载。
      Parameters:
      file - 文件
      Throws:
      IllegalArgumentException - 文件不存在或加载失败
  • Method Details

    • load

      public Props load(String path)
      从 classpath 相对路径加载(UTF-8)。
      Parameters:
      path - classpath 相对路径
      Returns:
      本对象
      Throws:
      IllegalArgumentException - 资源不存在或加载失败
    • load

      public Props load(File file)
      从文件加载(UTF-8)。
      Parameters:
      file - 文件
      Returns:
      本对象
      Throws:
      IllegalArgumentException - 文件不存在或加载失败
    • getStr

      public String getStr(String key)
      读取字符串,不存在返回 null。
      Parameters:
      key - 键
      Returns:
      值或 null
    • getStr

      public String getStr(String key, String defaultValue)
      读取字符串,不存在或为空返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getInt

      public int getInt(String key, int defaultValue)
      读取 int,不存在或解析失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getLong

      public long getLong(String key, long defaultValue)
      读取 long,不存在或解析失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getDouble

      public double getDouble(String key, double defaultValue)
      读取 double,不存在或解析失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getBool

      public boolean getBool(String key, boolean defaultValue)
      读取 boolean,不存在返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getBigDecimal

      public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
      读取 BigDecimal,不存在或解析失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值或默认值
    • getObj

      public Object getObj(String key)
      读取原始对象(字符串值)。
      Parameters:
      key - 键
      Returns:
      值或 null
    • toBean

      public <T> T toBean(Class<T> beanClass)
      转 Bean:属性名 → Bean 属性(带类型转换)。
      Type Parameters:
      T - Bean 类型
      Parameters:
      beanClass - Bean 类
      Returns:
      Bean 实例
    • toMap

      public Map<String,String> toMap()
      转为 Map 快照(键值均为字符串)。
      Returns:
      Map 快照
    • load

      public void load(InputStream in) throws IOException
      以 UTF-8 覆盖重载:Properties.load(InputStream) 默认 ISO-8859-1。
      Overrides:
      load in class Properties
      Parameters:
      in - 输入流
      Throws:
      IOException - IO 异常