歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中使用配置文件——properties類

Java中使用配置文件——properties類

日期:2017/3/1 9:33:55   编辑:Linux編程

在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類中直接調用該屬性獲取其配置值。

Copyright © Linux教程網 All Rights Reserved