package org.siyn.util;
import java.io.InputStream;
import java.util.Properties;
/**
* <p>
* 本類用提供一個線程同步的方法,讀取資源文件中的配置信息
* </p>
*
* @author siyn
* @date 2008-7-10
*/
public class PropertiesReader
{
private String file;
private Properties properties;
/**
* 構造 PropertysReader
* @param {String} path 相對于classes的文件路徑
*/
public PropertiesReader(String path)
{
this.file = path;
this.properties = new Properties();
}
/**
* <p>
* 本方法根據資源名獲取資源內容
* <p>
*
* @param {String} key 資源文件內key
* @param {Stirng} defaultValue 默認值
*
* @reaurn String key對應的資源內容
*/
public synchronized String getProperty(String key, String defaultValue)
{
try
{
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream(this.file);
properties.load(in);
}
catch (Exception ex1)
{
System.out.println("沒有找到資源文件:" + this.file);
}
return properties.getProperty(key, defaultValue);
}
/**
* <p>
* 本方法根據資源名獲取資源內容
* <p>
*
* @param {String} key 資源文件內key
* @param {Stirng} defaultValue 默認值
* @param {boolean} isnull 如果配置文件value為空,是否使用默認值
*
* @reaurn String key對應的資源內容
*/
public synchronized String getProperty(String key, String defaultValue,boolean isnull)
{
String value = null;
value = getProperty(key,defaultValue);
if(isnull && value == null && "".equals(value.trim()) )
value = defaultValue;
return value;
}
public static void main(String[] args)
{
PropertiesReader preader = new PropertiesReader("log4j.properties");
String rootLogger = preader.getProperty("aaa", "defaul");
System.out.println(rootLogger);
}
}