yj10864

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            8 Posts :: 1 Stories :: 9 Comments :: 0 Trackbacks
           1import java.io.FileInputStream;
           2import java.io.FileOutputStream;
           3import java.util.HashMap;
           4import java.util.Iterator;
           5import java.util.Properties;
           6
           7
           8/**
           9 * 功能:解析資源文件
          10 *
          11 */

          12public class PropertyUtil {
          13    
          14    private Properties property=new Properties();
          15    private String filepath=""
          16    
          17    /**
          18     * 解析資源文件的類的構造函數
          19     * @param path 文件索在的包,如:com.cn.myproperty.dbo.properties
          20     */

          21    public PropertyUtil(String path){
          22        try {
          23            filepath=this.getClass().getResource("/").getPath();
          24            filepath=filepath+path;
          25            
          26            
          27            property.load(new FileInputStream(filepath));
          28        }
           catch (Exception e) {
          29            e.printStackTrace();
          30        }

          31    }

          32
          33    
          34    /**
          35     * 返回資源文件的hashmap對象
          36     */

          37    public HashMap getElements(){
          38        HashMap map=new HashMap ();
          39        Iterator it=this.property.keySet().iterator();
          40        while(it.hasNext()){
          41            String key = it.next().toString();
          42            map.put(key, property.getProperty(key));
          43        }

          44        return map; 
          45    }

          46    
          47    
          48    //返回資源文件里的值
          49    public String getvalue(String key){
          50        if(key!=null)
          51            return this.property.getProperty(key);
          52        else
          53            return "";
          54    }

          55    
          56    //設置資源文件里的值
          57    public boolean setvalue(String key,String value){
          58        if(key!=null)
          59            {
          60                try{
          61                    this.property.setProperty(key,value);
          62                    this.property.store(new FileOutputStream(filepath), filepath);
          63                    return true;
          64                }
          catch(Exception ex){
          65                    System.out.println("保存資源文件出錯了"+this);
          66                }

          67            }

          68        return false;
          69    }

          70    
          71
          72}

          73
           1import java.util.HashMap;
           2
           3
           4public class ConfigManager {
           5    private static HashMap map=null;
           6    static{
           7        if(map==null){
           8            PropertyUtil propertyUtil = new PropertyUtil("property.properties");
           9            map=propertyUtil.getElements();
          10        }

          11    }

          12    /**
          13     * 得到SystemConfig資源文件中定義的值,如果未定義則返回空字符串,不是null
          14     * @param key
          15     * @return
          16     */

          17    public static String getValue(String key){
          18        try{
          19            return map.get(key).toString();
          20        }
          catch(Exception ex){
          21            System.out.println(ConfigManager.class+">>> zi yuan wen jian mei you ding yi key suo dui ying de zhi");
          22            return "";
          23        }

          24    }

          25    
          26}
          頁面使用:
          1ConfigManager.getValue(******)
          posted on 2010-01-08 13:51 jerry yang 閱讀(649) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 朝阳县| 吉首市| 怀来县| 九台市| 三台县| 宽城| 丰台区| 瓦房店市| 兴宁市| 象州县| 珠海市| 闽侯县| 济阳县| 汉沽区| 永德县| 尤溪县| 深水埗区| 苍梧县| 方正县| 祥云县| 社会| 松滋市| 延安市| 邓州市| 马关县| 黎城县| 南汇区| 七台河市| 南宁市| 大渡口区| 伽师县| 孟连| 五峰| 和静县| 汉中市| 玉林市| 南安市| 临汾市| 曲松县| 绥阳县| 阳东县|