Class Props

java.lang.Object
com.sure.tool.util.Props

public class Props extends Object
Properties 配置文件增强工具:UTF-8 加载、强类型读取、默认值,零依赖。

覆盖 .properties 中文乱码场景:加载时按指定字符集读取(默认 UTF-8)。

Since:
0.2.0
Author:
suretool
  • Constructor Details

    • Props

      public Props()
      构造空配置。
    • Props

      public Props(String path) throws IOException
      从路径加载(UTF-8)。
      Parameters:
      path - 文件路径
      Throws:
      IOException - IO 异常
    • Props

      public Props(File file) throws IOException
      从文件加载(UTF-8)。
      Parameters:
      file - 文件
      Throws:
      IOException - IO 异常
    • Props

      public Props(InputStream in) throws IOException
      从输入流加载(UTF-8)。
      Parameters:
      in - 输入流
      Throws:
      IOException - IO 异常
  • Method Details

    • load

      public Props load(File file) throws IOException
      加载文件(UTF-8)。
      Parameters:
      file - 文件
      Returns:
      this
      Throws:
      IOException - IO 异常
    • load

      public Props load(InputStream in) throws IOException
      加载输入流(UTF-8)。
      Parameters:
      in - 输入流
      Returns:
      this
      Throws:
      IOException - IO 异常
    • load

      public Props load(InputStream in, Charset charset) throws IOException
      加载输入流(指定字符集,避免中文乱码)。
      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      this
      Throws:
      IOException - IO 异常
    • getStr

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

      public String getStr(String key, String defaultValue)
      取字符串(带默认值)。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      值
    • getInt

      public int getInt(String key)
      取 int。
      Parameters:
      key - 键
      Returns:
      int 值;缺失返回 0
    • getInt

      public int getInt(String key, int defaultValue)
      取 int(带默认值)。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      int 值
    • getLong

      public long getLong(String key)
      取 long。
      Parameters:
      key - 键
      Returns:
      long 值;缺失返回 0
    • getLong

      public long getLong(String key, long defaultValue)
      取 long(带默认值)。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      long 值
    • getDouble

      public double getDouble(String key)
      取 double。
      Parameters:
      key - 键
      Returns:
      double 值;缺失返回 0
    • getDouble

      public double getDouble(String key, double defaultValue)
      取 double(带默认值)。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      double 值
    • getBool

      public boolean getBool(String key)
      取 boolean(true/1/yes 视为 true,忽略大小写)。
      Parameters:
      key - 键
      Returns:
      boolean 值;缺失返回 false
    • getBool

      public boolean getBool(String key, boolean defaultValue)
      取 boolean(带默认值)。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      boolean 值
    • containsKey

      public boolean containsKey(String key)
      是否包含键。
      Parameters:
      key - 键
      Returns:
      是否包含
    • set

      public Props set(String key, Object value)
      设置键值。
      Parameters:
      key - 键
      value - 值
      Returns:
      this
    • getProperties

      public Properties getProperties()
      获取底层 Properties。
      Returns:
      Properties
    • toString

      public String toString()
      Overrides:
      toString in class Object