Java遍历Properties所有元素的方法实例

2025-05-27 0 43

代码如下:


//初始化properties

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
———————————

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java遍历Properties所有元素的方法实例 https://www.kuaiidc.com/77470.html

相关文章

发表评论
暂无评论