【深入JAVA】Java中使用配置文件——properties类

来源:互联网 发布:js防水涂料多少钱一桶 编辑:程序博客网 时间:2024/06/07 00:35

在阅读的过程中有任何问题,欢迎一起交流

邮箱:1494713801@qq.com   

QQ:1494713801

 

 

    在java.util 包下面有一个类 Properties,该类主要用于读取项目中的配置文件(以.properties结尾的文件和xml文件)。

使用示例:

Java类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

   InputStream inputStream = Agent.class.getClassLoader().getResourceAsStream("config.properties");
   Properties properties = new Properties();
   properties.load(inputStream);
   String time = properties.getProperty("period");

配置文件config.properties:

#timer
period=30

该示例表示在config.properties中配置属性period的值为30,在java类中直接调用该属性获取其配置值。
0 0
原创粉丝点击