讀取配置文件*.properties中的信息

public class PropertiesUtil {


// 讀取配置文件中的信息
private static Properties config = null;


/**

* @title queryProperties
* @description 讀取配置文件definevariable.properties中的信息
* @create_date Jan 8, 2012
* @throws IOException
*/
public static Properties queryProperties() throws IOException {
PropertiesUtil propertiesUtil = new PropertiesUtil();
InputStream in = propertiesUtil.getClass().getClassLoader()
.getResourceAsStream("definevariable.properties");
config = new Properties();
try {
config.load(in);
in.close();
} catch (IOException e) {
throw new IOException();
}
return config;
}


/**

* @title queryProperties
* @description 根據配置文件名字讀取配置文件中的信息
* @create_date Jan 10, 2012
* @param name
* @return
* @throws IOException
*/
public static Properties queryProperties(String name) throws IOException {
PropertiesUtil propertiesUtil = new PropertiesUtil();
InputStream in = propertiesUtil.getClass().getClassLoader()
.getResourceAsStream(name);
config = new Properties();
try {
config.load(in);
in.close();
} catch (IOException e) {
throw new IOException();
}
return config;
}


/**

* @title queryPropertiesValue
* @description 讀取definevariable.properties配置文件中的變量並放入List中
* @create_date Feb 13, 2012
* @param property
*            配置文件中的變量名稱
* @return List
* @throws IOException
*/
@SuppressWarnings( { "static-access", "unchecked" })
public static List<String> queryPropertiesValue(String property)
throws IOException {
List<String> list = new ArrayList<String>();
String useStyle;
try {
PropertiesUtil propertiesUtil = new PropertiesUtil();
// 讀取配置文件中信息
useStyle = propertiesUtil.queryProperties().getProperty(property);
String[] userStyleArr = useStyle.split(",");
for (int i = 0; i < userStyleArr.length; i++) {
list.add(userStyleArr[i]);
}
} catch (IOException e) {
throw new IOException();
}
return list;
}


}
發佈了29 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章