java应用中读取配置文件完整示例

package com.jcms.cms.core;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author runningsnail
* @date 2009-12-28
*
*/
public class GlobalPropertiesLoader {
private static final Logger log = LoggerFactory
.getLogger(GlobalPropertiesLoader.class);
private static final String DEFAULT_FILENAME = "default.properties";
private static Properties properties = new Properties();

static {

// 1. load library defaults
InputStream in = GlobalPropertiesLoader.class
.getResourceAsStream(DEFAULT_FILENAME);

if (in == null) {
log.error("{} not found", DEFAULT_FILENAME);
throw new RuntimeException(DEFAULT_FILENAME + " not found");
} else {
if (!(in instanceof BufferedInputStream))
in = new BufferedInputStream(in);

try {
properties.load(in);
in.close();
log.info("{} loaded", DEFAULT_FILENAME);
} catch (Exception e) {
log.error("Error while processing {}", DEFAULT_FILENAME);
throw new RuntimeException("Error while processing "
+ DEFAULT_FILENAME, e);
}
}

}

/**
* Searches for the property with the specified key in this property list.
*
* @see Properties#getProperty(String)
*/
public static String getProperty(final String key) {
return properties.getProperty(key);
}

/**
* Sets the property with the specified key in this property list.
*
* @see Properties#setProperty(String, String)
*/
public static void setProperty(final String key, final String value) {
properties.setProperty(key, value);
}

/**
* Returns content path
*/
public static String getContextPhyPath() {
return properties.getProperty("app.context.phy.path");
}

public static String getFullTextIndexPath(){
return properties.getProperty("search.fulltext.index.path");
}

public static String getProjectPath(){
return properties.getProperty("app.context.project.path");
}

public static String getTemplateExtension(){
return properties.getProperty("app.context.template.extension");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值