屬性文件讀取類
package com.yanek.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public final class Global {
public static void main(String[] args)
{
System.out.println(Global.exptype);
System.out.println(Global.descDic);
System.out.println(Global.sourceDic);
}
/** 日誌記錄 */
static Logger log = Logger.getLogger(Global.class.getName());
/** 應用程序配置參數緩存 */
private static Properties appPropertiesCache;
/** 服務配置文件名 */
//private static String config_file = "conf/mail.properties";
private static String config_file = "config.properties";
public static String APPLICATION_ROOT = null;
public static String exptype = null; //出庫類型
public static String sourceDic = null; //來源目錄
public static String descDic = null; //目標目錄
static {
try {
APPLICATION_ROOT = System.getProperty("user.dir");
System.out.println("properties file:" + config_file);
appPropertiesCache = loadProperties(config_file);
sourceDic = appPropertiesCache.getProperty("sourceDic");
descDic=appPropertiesCache.getProperty("descDic");
exptype=appPropertiesCache.getProperty("exptype");
} catch (IOException e) {
e.printStackTrace();
}
}
/***********************************************************************************************
* 根據給定名字查找資源,返回其輸入流
*
* @param uri
* 資源名
* @return 輸入流
*/
public static InputStream getResourceAsStream(String uri) {
return Global.class.getClassLoader().getResourceAsStream(uri);
}
/***********************************************************************************************
* 載入屬性文件
*
* @param 文件基於類路徑的相對路徑名
* @return 根據屬性文件生成的Properties對象
* @throws IOException
*/
public static Properties loadProperties(String uri) throws IOException {
Properties result = null;
InputStream in = getResourceAsStream(uri);
if (in != null) {
try {
result = new Properties();
result.load(in);
} finally {
in.close();
//ResourceMgr.closeQuietly(in);
}
} else
throw new IOException("載入屬性文件失敗!" + uri);
return result;
}
/***********************************************************************************************
* 根據指定鍵值取應用程序配置屬性
*
* @param key
* 該屬性在配置文件中的鍵
* @return 該屬性在配置文件中的字符串值
*/
public static String getAppProperty(String key) {
String value = Global.appPropertiesCache.getProperty(key);
if (value == null)
value = System.getProperty(key);
return value;
}
}
注意:配置文件在src根目錄即可
config.properties
exptype=2
sourceDic=C://artistExp//in
descDic=C://artistExp//out
#descDic=////192.168.61.172//temp
#descDic=////192.168.61.172//temp
systype=new
最後更新:2017-04-02 06:51:30