java 读取 web-info_java读取web工程下的web-info路径

当我们在做web工程时总会用到读取配置文件

那么一般把配置文件放在类同目录是比较好读取的

Properties config = new Properties();

InputStream in = ReadProp.class.getResourceAsStream(fileName);

config.load(in);

但是如果放在指定的工程下统一路径那么如何读取指定路径呢.

下面介绍一个比较好的方法.

/**

* @Descrption : 获取当前web工程部署路径并找到config文件夹

* @return :String有则返回否则空

* @Version:1.0

* @Date:2012-6-8

* @Author:lixw

*/

public static String getWEBINFOPath(){

String configpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();

if (StringUtils.hasContent(configpath)) {

configpath = configpath.replace("classes/", "config/");

configpath = configpath.substring(1, configpath.length());

}else{

configpath="";

}

return configpath;

}

/**

* @Descrption : 根据文件路径,需要读取的配置文件key 获得 key下的值

* @param fileName

*            文件路径名

* @param configName

*            key名称

* @return :String 返回配置文件对应key下的值

* @Version:1.0

* @Date:2012-6-8

* @Author:lixw

*/

public static String getConfigFields(String fileName, String configName) {

String getFields = "";

try {

String configpath = StringUtils.getWEBINFOPath();

InputStream in = null;

if (StringUtils.hasContent(configpath)) {

in = new BufferedInputStream(new FileInputStream(configpath + fileName));// 读取指定目录下的配置文件

} else {

in = ReadProp.class.getResourceAsStream(fileName);// 读取当前类路径下的文件

}

config.load(in);

getFields = config.getProperty(configName);

} catch (Throwable t) {

logger.error(t);

t.printStackTrace();

throw new ExceptionInInitializerError();

}

return getFields;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值