Java遍历Properties所有元素的方法实例
时间:2022-09-08 14:58:24
//初始化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
---------------------------------
标签:Properties,java
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈c# 浮点数计算
2023-08-04 22:10:09
![](https://img.aspxhome.com/file/2023/8/111288_0s.png)
详解Java数据结构之平衡二叉树
2022-05-06 08:54:13
![](https://img.aspxhome.com/file/2023/1/78061_0s.png)
Java MultipartFile实现上传文件/上传图片
2022-04-19 07:07:19
Android GridView实现滚动到指定位置的方法
2021-08-29 10:04:21
解析android中include标签的使用
2022-08-17 06:59:59
![](https://img.aspxhome.com/file/2023/0/139220_0s.png)
MyBatis-Plus拦截器实现数据权限控制的示例
2022-12-10 05:10:17
c#操作Redis的5种基本类型汇总
2021-06-03 03:24:07
![](https://img.aspxhome.com/file/2023/7/89807_0s.png)
Android自定义ActionProvider ToolBar实现Menu小红点
2022-09-09 05:07:30
![](https://img.aspxhome.com/file/2023/9/98649_0s.gif)
spring boot线上日志级别动态调整的配置步骤
2022-09-19 01:57:45
![](https://img.aspxhome.com/file/2023/2/94312_0s.png)
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
使用chatgpt实现微信聊天小程序的代码示例
2022-04-26 17:18:24
![](https://img.aspxhome.com/file/2023/0/67500_0s.jpg)
springboot动态定时任务的实现方法示例
2023-04-20 15:59:58
Spring Bean 依赖注入常见错误问题
2022-10-02 20:46:51
![](https://img.aspxhome.com/file/2023/5/65755_0s.jpg)
Android对话框使用方法详解
2023-11-09 03:37:51
![](https://img.aspxhome.com/file/2023/1/130421_0s.png)
浅析java中Integer传参方式的问题
2023-10-27 03:35:43
Java运行时数据区概述详解
2023-10-08 07:00:10
![](https://img.aspxhome.com/file/2023/0/69130_0s.png)
Java实现选择排序
2021-06-30 16:53:55
![](https://img.aspxhome.com/file/2023/2/84602_0s.jpg)
详解Java使用Pipeline对Redis批量读写(hmset&hgetall)
2023-11-17 15:17:24
OpenCV画任意圆弧曲线
2023-06-22 19:28:44
![](https://img.aspxhome.com/file/2023/5/94045_0s.jpg)
Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)的过程解析
2021-10-03 11:25:56
![](https://img.aspxhome.com/file/2023/1/72651_0s.jpg)