Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
它提供了几个主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
package com.demo.util;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*JAVA读写Properties属性文件公用方法
*/
public class PropertieUtil
{
/**
* 日志记录器
*/
private static Logger logger = Logger.getLogger(PropertieUtil.class);
/**
* <默认构造函数>
*/
public PropertieUtil() {
}
/**
* 根据配置文件key读取配置文件value
* @param filepath 配置文件路径
* @param key 配置文件key
* @return
* @see [类、类#方法、类#成员]
*/
public static String readProperties(String filepath,String key){
try {
Properties properties = new Properties();
// 注意路径以 / 开始,没有则处理
if(!filepath.startsWith("/")){
filepath = "/"+filepath;
}
// 加载filepath 获取inputstream输入流
InputStream in = PropertieUtil.class.getResourceAsStream(filepath);
properties.load(in);
return properties.getProperty(key);
} catch (Exception e) {
logger.error(e);
}
return null;
}
/**
* 打印所有配置文件配置项
* @param filepath 配置文件路径
* @see [类、类#方法、类#成员]
*/
public static void printProperties(String filepath){
try {
Properties properties = new Properties();
// 注意路径以 / 开始,没有则处理
if(!filepath.startsWith("/")){
filepath = "/"+filepath;
}
// 加载filepath 获取inputstream输入流
InputStream in = PropertieUtil.class.getResourceAsStream(filepath);
properties.load(in);
Enumeration<?> elements = properties.propertyNames();
while (elements.hasMoreElements()) {
// 获取配置key
String key = (String) elements.nextElement();
logger.info("load filepath:"+ filepath +" successed");
logger.info(key +":"+ properties.getProperty(key));
}
} catch (Exception e) {
logger.error(e);
}
}
public static void main( String[] args )
{
String key = readProperties("config.properties", "user");
System.out.println(key);
printProperties("config.properties");
}
}
程序运行结果:
xiaoqiu
2017-01-04 15:36:08[INFO][com.demo.util.PropertieUtil] [com.demo.util.PropertieUtil.printProperties(PropertieUtil.java:79)] - load filepath:/config.properties successed
2017-01-04 15:36:08[INFO][com.demo.util.PropertieUtil] [com.demo.util.PropertieUtil.printProperties(PropertieUtil.java:80)] - user:xiaoqiu
2017-01-04 15:36:08[INFO][com.demo.util.PropertieUtil] [com.demo.util.PropertieUtil.printProperties(PropertieUtil.java:79)] - load filepath:/config.properties successed
2017-01-04 15:36:08[INFO][com.demo.util.PropertieUtil] [com.demo.util.PropertieUtil.printProperties(PropertieUtil.java:80)] - password:xiaoqiu