日期:2014-05-20  浏览次数:20754 次

[闭月羞花猫]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);