`
piperzero
  • 浏览: 3479589 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

属性文件读取类

 
阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics