Class Properties

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>, org.aoju.bus.core.getter.TypeGetter<CharSequence>

public final class Properties extends Properties implements org.aoju.bus.core.getter.TypeGetter<CharSequence>
Properties文件读取封装类
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • Properties

      public Properties(String path)
      构造,使用相对于Class文件根目录的相对路径
      Parameters:
      path - 配置文件路径,相对于ClassPath,或者使用绝对路径
    • Properties

      public Properties(String path, Charset charset)
      构造,使用相对于Class文件根目录的相对路径
      Parameters:
      path - 相对或绝对路径
      charset - 自定义编码
    • Properties

      public Properties(File propertiesFile)
      构造
      Parameters:
      propertiesFile - 配置文件对象
    • Properties

      public Properties()
      构造
    • Properties

      public Properties(File propertiesFile, Charset charset)
      构造
      Parameters:
      propertiesFile - 配置文件对象
      charset - 自定义编码
    • Properties

      public Properties(org.aoju.bus.core.io.resource.Resource resource, Charset charset)
      构造,使用URL读取
      Parameters:
      resource - Resource
      charset - 自定义编码
    • Properties

      public Properties(Properties properties)
      构造,使用URL读取
      Parameters:
      properties - 属性文件路径
  • Method Details

    • of

      public static Properties of()
      构建一个空的Props,用于手动加入参数
      Returns:
      Properties
    • of

      public static Properties of(String resource)
      获得Classpath下的Properties文件
      Parameters:
      resource - 资源(相对Classpath的路径)
      Returns:
      Props
    • of

      public static Properties of(String resource, Charset charset)
      获得Classpath下的Properties文件
      Parameters:
      resource - 资源(相对Classpath的路径)
      charset - 自定义编码
      Returns:
      Properties
    • load

      public void load(URL url)
      初始化配置文件
      Parameters:
      url - URL
    • load

      public static void load(Properties properties, org.aoju.bus.core.io.resource.Resource resource, Charset charset)
      加载配置文件内容到Properties中 需要注意的是,如果资源文件的扩展名是.xml,会调用Properties.loadFromXML(InputStream) 读取。
      Parameters:
      properties - Properties文件
      resource - 资源
      charset - 编码,对XML无效
    • load

      public void load(org.aoju.bus.core.io.resource.Resource resource)
      初始化配置文件
      Parameters:
      resource - Resource
    • load

      public void load()
      重新加载配置文件
    • autoLoad

      public void autoLoad(boolean autoReload)
      在配置文件变更时自动加载
      Parameters:
      autoReload - 是否自动加载
    • getObject

      public Object getObject(CharSequence key, Object defaultValue)
      Specified by:
      getObject in interface org.aoju.bus.core.getter.TypeGetter<CharSequence>
    • get

      public <P, T> T get(org.aoju.bus.core.lang.function.XFunction<P,T> func)
      根据lambda的方法引用,获取
      Type Parameters:
      P - 参数类型
      T - 返回值类型
      Parameters:
      func - 方法引用
      Returns:
      获取表达式对应属性和返回的对象
    • getAndRemoveString

      public String getAndRemoveString(String... keys)
      获取并删除键值对,当指定键对应值非空时,返回并删除这个值,后边的键对应的值不再查找
      Parameters:
      keys - 键列表,常用于别名
      Returns:
      字符串值
    • toProperties

      public Properties toProperties()
      转换为标准的Properties对象
      Returns:
      Properties对象
    • toBean

      public <T> T toBean(Class<T> beanClass)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      beanClass - Bean类
      Returns:
      Bean对象
    • toBean

      public <T> T toBean(Class<T> beanClass, String prefix)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      beanClass - Bean类
      prefix - 公共前缀,不指定前缀传null,当指定前缀后非此前缀的属性被忽略
      Returns:
      Bean对象
    • fillBean

      public <T> T fillBean(T bean, String prefix)
      将配置文件转换为Bean,支持嵌套Bean 支持的表达式:
       persion
       persion.name
       persons[3]
       person.friends[5].name
       ['person']['friends'][5]['name']
       
      Type Parameters:
      T - Bean类型
      Parameters:
      bean - Bean对象
      prefix - 公共前缀,不指定前缀传null,当指定前缀后非此前缀的属性被忽略
      Returns:
      Bean对象
    • set

      public void set(String key, Object value)
      设置值,无给定键创建之 设置后未持久化
      Parameters:
      key - 属性键
      value - 属性值
    • setFields

      public Properties setFields(org.aoju.bus.core.lang.function.XSupplier<?>... fields)
      通过lambda批量设置值 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
           User user = GenericBuilder.of(User::new).with(User::setUsername, "bus").build();
           Setting.of().setFields(user::getNickname, user::getUsername);
       
      Parameters:
      fields - lambda,不能为空
      Returns:
      this
    • store

      public void store(String absolutePath) throws org.aoju.bus.core.exception.InternalException
      持久化当前设置,会覆盖掉之前的设置
      Parameters:
      absolutePath - 设置文件的绝对路径
      Throws:
      org.aoju.bus.core.exception.InternalException - IO异常,可能为文件未找到
    • store

      public void store(String path, Class<?> clazz)
      存储当前设置,会覆盖掉以前的设置
      Parameters:
      path - 相对路径
      clazz - 相对的类