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 阅读(629) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: