[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[java]如何遍历Properties的所有的元素-----Listing the Elements of a Properties

上一篇:[java]java.util.Calendar的属性DAY_OF_WEEK_IN_MONTH
下一篇:[java]properties应该放在哪里?-----Where do I put .properties files?

添加日期:2006/6/28 9:01:38 快速返回   返回列表 阅读9681次


Properties pro = new Properties();
try {
    InputStream inStr = ClassLoader.getSystemResourceAsStream("wahaha.properties");
    pro.load(inStr);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

//propertyNames(),返回属性列表中所有键的枚举
Enumeration enu2=pro.propertyNames();
while(enu2.hasMoreElements()){
    String key = (String)enu2.nextElement();
    System.out.println(key);


//Properties 继承于 Hashtable,elements()是Hashtable的方法,返回哈希表中的值的枚举。
Enumeration enu=pro.elements();
while(enu.hasMoreElements()){
    String key = (String)enu.nextElement();
    System.out.println(key);

//Properties 继承于 Hashtable,entrySet()是Hashtable的方法,
//返回此 Hashtable 中所包含的键的 Set 视图。此 collection 中每个元素都是一个 Map.Entry
Iterator it=pro.entrySet().iterator();
while(it.hasNext()){
    Map.Entry entry=(Map.Entry)it.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
    System.out.println(key +":"+value);
}


假设wahaha.properties中内容为:
------------------------------
name1=xxxx
name2=yyyyy
name3=zzzzzzz
------------------------------

上面的代码将会输出:
--------------------------
name1
name2
name3
xxxx
yyyyy
zzzzzzz
name1:xxxx
name2:yyyyy
name3:zzzzzzz
---------------------------------
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved