[闭月羞花猫]web开发中,如何取得项目定义的property文件。
闲来无事,写了几篇关于web开发过程中,一些基础的应用。
希望能够对初学者有所帮助。
不足的地方,也希望高手能够给与指正。
property文件名称:properyName.properties;位置:项目的classes目录下面。
内容:AAA=luyang
(取得时候,使用的是类路径)
外部调用方法:
PropMngr mgr = PropMngr .getInstance();
String content = getProperty( "AAA ");
// AAA就是property中定义的key。 得到的结果就是:luyang
=======================
package org.luyang;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* property管理
*
* @version 1.0
* @author luyang
*/
public class PropMngr {
private Properties props = new Properties();
public static PropMngr instances = null;
public static String NAME = "properyName ";
public PropMngr getInstance() {
if (null == instances) {
instances = new PropMngr();
}
return instances;
}
/**
* 私有的构造函数
*/
private PropMngr() {
init(NAME);
}
/**
* property的初期化
* @param sPropFilePathName property的class路径
* @throws MissingResourceException
*/
public synchronized void init(String sPropFilePathName)
throws MissingResourceException {
String propFile = sPropFilePathName;
ResourceBundle bundle = ResourceBundle.getBundle(propFile);
Enumeration enume = bundle.getKeys();
Object key = null;
Object value = null;
while (enume.hasMoreElements()) {
key = enume.nextElement();
value = bundle.getString(key.toString());
props.put(key, value);
}
// debug使用
Iterator it = props.entrySet().iterator();
while (it.hasNext()) {
Map.Entry xx= (Map.Entry)it.next();
System.out.println(xx.getKey());
System.out.println(xx.getValue());
}
}
/**
* 取得property
*
* @param key prpperty的key
* @return String prpperty内容
*/
public String getProperty(String key) {
return props.getProperty(key);
}
}
[解决办法]
我搜藏了,强烈的顶
[解决办法]
how to load your properties files smartly
please refer to below hyperlink:
http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html?page=1
====================================================================================
来拿猫的分的,哈哈!
[解决办法]
顶你来了.
[解决办法]
接分接分,好多人不结帐,穷死我了
[解决办法]
顶一下~~~~