使用Properties读取配置文件的示例详解

 更新时间:2023年06月26日 10:17:05   作者:东方甲乙木土  
开发SpringBoot项目时,使用配置文件配置项目相关属性是必不可少的,所以下文为大家准备了使用Properties读取配置文件的示例代码,希望对大家有所帮助

1. 配置文件信息

1.1 项目默认配置文件

开发SpringBoot项目时,使用配置文件配置项目相关属性是必不可少的,SpringBoot也为我们提供了一些默认的配置文件格式。

  • application.properties,以键值对的方式配置属性值
  • application.yml:以数据对象的方式配置属性值

配置文件存放位置:src/resource/

按照SpringBoot给定的格式和位置创建文件并定义相关属性值后,SpringBoot项目会在启动时自动加载属性值。

1.2 自定义配置文件

除了官方指定的配置文件,我们还可以自定义一个配置文件,并在合适的时候使用其中的配置信息,如db.properties、jdbc.properties、mybatis.properties。

但是由于SpringBoot对application.properties配置文件的自动加载,大部分的属性定义都可以在主配置文件中完成了,并使用@value等方法获取配置内容。

但是如果我们需要将一系列相关配置放在一个单独的.properties文件中,这种情况下如何在项目中加载配置文件并读取其中内容呢?这个时候就需要Properties出场了。

2. Properties

2.1 认识Properties类

Properties类对应SpringBoot中的application.properties文件,定义为一系列属性的集合,可以从流中读取属性或将属性保存到流中,且属性中值都是字符串格式。

Properties类作为一个集合,继承了Hashaable类,因此是线程安全的。

class Properties extends Hashtable<Object,Object>{
    ...
    public Properties() {
        this(null);
    }
    ...
}

Properties初始化实例时也比较简单,只需要使用new关键字调用类的无参构造方法创建对象即可。

Properties properties = new Properties();

2.2 读取流

Properties类中提供了load()方法来读取流中的内容,创建对象后,调用对象的load()方法,并提供字节流作为参数,就可以读取字节中属性内容。

简要的读取流程可以描述为:

  • 创建Properties类对象
  • 使用对象的load()方法读取流文件
  • 对象中读取属性数据后,使用getProperty()方法获取属性值

根据方法参数的不同,Properties中定义了两个同名的load()方法,两种方法本质上都是调用了内部的load0()方法,用来从字节流中获取数据内容。

//以Reader对象作为参数读取
public synchronized void load(Reader reader) throws IOException {
    load0(new LineReader(reader));
}
​
//以InputStream作为参数读取
public synchronized void load(InputStream inStream) throws IOException {
    load0(new LineReader(inStream));
}

读取到配置文件后,Properties对象还提供了getProperty(String key)方法来获取指定属性的值,如果在配置中当前属性不存在,则返回null值;

或者使用另一个getProperty(String key, String defaultValue)方法在获取属性时指定一个默认值,如果属性不存在则使用指定值代替null。

//获取key对应值,如果key不存在,则返回null
public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
​
//如果不存在key,则返回defaultValue
public String getProperty(String key, String defaultValue) {
    String val = getProperty(key);
    return (val == null) ? defaultValue : val;
}

2.3 输出到流

使用Properties对象将属性输出到字节流中的流程可以表示为:

  • 实例化一个Properties对象,
  • Properties对象使用setProperty()方法设置属性和值,
  • 调用对象的store()方法将对象通过字节流保存到相应文件。

对应的方法定义有:

//设置属性,本质是Hashtable的put方法
public synchronized Object setProperty(String key, String value) {
    return put(key, value);
}
​
//输出为字符流对象Writer
public void store(Writer writer, String comments)
        throws IOException
{
    store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
           : new BufferedWriter(writer),
           comments,
           false);
}
​
//输出为OutputStream流
public void store(OutputStream流 out, String comments)
        throws IOException
{
    store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
           comments,
           true);
}

3. 读取配置文件的几种方式

了解Properties读取文件的流程后,来看一下实际在项目中读取自定义配置文件内容的方式都有哪些。

1.类的getResourceAsStream方法

//类的getResourceAsStream方法,路径加"/"时代表读取resources目录下内容
InputStream inputStream = PropertiesUtils.class.getResourceAsStream方法,路径加"/"时代表读取resources目录下内容("/test.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("test.content"));

2.类启动器的getResourceAsStream方法

//类启动器的getResourceAsStream方法,默认获取路径为src/resources下的文件
InputStream inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("test.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("test.content"));

3.PropertiesLoaderUtils工具类的loadAllProperties()方法

// PropertiesLoaderUtils类的loadAllProperties()方法,读取resources目录
Properties properties = PropertiesLoaderUtils.loadAllProperties("test.properties");
System.out.println(properties.getProperty("test.content"));

以上就是使用Properties读取配置文件的示例详解的详细内容,更多关于Properties读取配置文件的资料请关注脚本之家其它相关文章!

相关文章

最新评论