PropertiesUtils:
package com.zhihua.config;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 属性文件读取工具类
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月11日]
*/
public class PropertiesUtils {
/**
* 默认属性集合(文件在Constants中配置)
*/
protected static Properties defaultProp = null;
/**
* 所有读取过的属性集合
* 文件名<->属性集合
*/
protected static Map<String,Properties> allProps = new HashMap<String,Properties>();
static{
if(defaultProp==null){
// 注意获取配置文件的路径 包名+文件名
defaultProp = loadProperties("com/zhihua/config/config.properties");
allProps.put("config.properties", defaultProp);
}
}
/**
* 读取属性文件,并将读出来的属性集合添加到【allProps】当中
* 如果该属性文件已经读取过,则直接从【allProps】获得
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param fileName
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月11日]
*/
public static Properties getProperties(String fileName) {
if(fileName == null || "".equals(fileName)){
return defaultProp;
}else{
Properties prop = allProps.get(fileName);
if(prop == null){
prop = loadProperties(fileName);
allProps.put(fileName, prop);
}
return prop;
}
}
/**
* 从指定的属性文件中获取某一属性值
* 如果属性文件不存在该属性则返回null
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param fileName 属性文件
* @param name 属性名
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月11日]
*/
public static String getProperty(String fileName, String name){
return getProperties(fileName).getProperty(name);
}
/**
* 从默认的属性文件中获取某一属性
* 如果属性文件不存在则该属性则返回 null
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param fileName
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月11日]
*/
public static String getProperty(String name){
return getProperties(null).getProperty(name);
}
/**
* 解析属性文件,将文件中的所有属性都读取到【Properties】当中
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param fileName
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月11日]
*/
private static Properties loadProperties(String fileName) {
Properties prop = new Properties();
InputStream inStream = null;
//PropertiesUtils.class.getClassLoader().getResourceAsStream()查找在src文件夹下的文件。
inStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName);
if(inStream == null){
System.err.println("Can not find the resource!");
}else{
try{
prop.load(inStream);
System.out.println("find the resource!");
}catch (IOException e) {
System.err.println("An error occurred when reading from the input stream, "+e.getMessage());
} catch (IllegalArgumentException e) {
System.err.println("The input stream contains a malformed Unicode escape sequence, "+e.getMessage());
}
}
return prop;
}
}
配置文件 config.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
测试类:
package com.zhihua.config.test;
import org.junit.Test;
import com.zhihua.config.PropertiesUtils;
public class PropertiesUtilsTest {
@SuppressWarnings("static-access")
@Test
public void loadProperties(){
PropertiesUtils propUtils = new PropertiesUtils();
/*String fileName = "/com/zhihua/config/config.properties";*/
String username = propUtils.getProperty("jdbc.username");//查看配置文件中jdbc.username的值
System.out.println(username);
}
}
亲测通过!