读取properties文件 并解析占位符(借用hibernate代码)

把hibernate中读取properties文件的代码抽取出来了,直接贴代码:
java:


package redolvePlaceHolder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

/**
* 此类借用了hibernate读取properties文件的代码,
* 可以解析占位符比如$
* @author afei
*
*/
public class TestMain {


private static final String PLACEHOLDER_START = "${";

private static Properties pro;

public static void main(String[] args) {
try {
pro=buildProperty("afei.properties");
resolvePlaceHolders(pro);
printProperties(pro);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void printProperties(Properties p){
Iterator it=p.entrySet().iterator();
while(it.hasNext()){
Entry ent=(Entry)it.next();
System.out.println(ent.getKey()+" : "+ent.getValue());
}
}



/**
* 解析占位符
* @param properties
*/
public static void resolvePlaceHolders(Properties properties) {
Iterator itr = properties.entrySet().iterator();
while ( itr.hasNext() ) {
final Map.Entry entry = ( Map.Entry ) itr.next();
final Object value = entry.getValue();
if ( value != null && String.class.isInstance( value ) ) {
final String resolved = resolvePlaceHolder( ( String ) value );
if ( !value.equals( resolved ) ) {
if ( resolved == null ) {
itr.remove();
}
else {
entry.setValue( resolved );
}
}
}
}
}

/**
* 解析占位符具体操作
* @param property
* @return
*/
public static String resolvePlaceHolder(String property) {
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
return property;
}
StringBuffer buff = new StringBuffer();
char[] chars = property.toCharArray();
for ( int pos = 0; pos < chars.length; pos++ ) {
if ( chars[pos] == '$' ) {
// peek ahead
if ( chars[pos+1] == '{' ) {
// we have a placeholder, spin forward till we find the end
String systemPropertyName = "";
int x = pos + 2;
for ( ; x < chars.length && chars[x] != '}'; x++ ) {
systemPropertyName += chars[x];
// if we reach the end of the string w/o finding the
// matching end, that is an exception
if ( x == chars.length - 1 ) {
throw new IllegalArgumentException( "unmatched placeholder start [" + property + "]" );
}
}
String systemProperty = extractFromSystem( systemPropertyName );
buff.append( systemProperty == null ? "" : systemProperty );
pos = x + 1;
// make sure spinning forward did not put us past the end of the buffer...
if ( pos >= chars.length ) {
break;
}
}
}
buff.append( chars[pos] );
}
String rtn = buff.toString();
return isEmpty( rtn ) ? null : rtn;
}



/**
* 构造properties文件
* @param path
* @throws IOException
*/
public static Properties buildProperty(String path) throws IOException{
InputStream is=getResourceAsStream("afei.properties");
pro=new Properties();
pro.load(is);
return pro;
}


/**
* 构造properties文件的流
* @param resource
* @return
*/
public static InputStream getResourceAsStream(String resource) {
String stripped = resource.startsWith("/") ?
resource.substring(1) : resource;

InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = TestMain.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = TestMain.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new RuntimeException( resource + " not found" );
}
return stream;
}


/**
* 获得系统属性 当然 你可以选择从别的地方获取值
* @param systemPropertyName
* @return
*/
private static String extractFromSystem(String systemPropertyName) {
try {
return System.getProperty( systemPropertyName );
}
catch( Throwable t ) {
return null;
}
}

/**
* 判断字符串的空(null或者.length=0)
* @param string
* @return
*/
public static boolean isEmpty(String string) {
return string == null || string.length() == 0;
}

}




afei.properties文件:

javaeye.url=www.iteye.com
javaeye.homepage=duyunfei.iteye.com
javaeye.username=${user.name}
javaeye.password=123456

home=${user.home}
os=${os.name}
userdir=${user.dir}
javaversion=${java.version}
javahome=${java.home}

fileseparator=${file.separator}
pathseparator=${path.separator}
lineseparator=${line.separator}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值