欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

IO流,Properties基本功能和遍历

程序员文章站 2022-06-21 13:52:54
1 import java.util.Enumeration; 2 import java.util.Iterator; 3 import java.util.Properties; 4 import java.util.Set; 5 6 public class PropertiesDemo { ... ......
 1 import java.util.Enumeration;
 2 import java.util.Iterator;
 3 import java.util.Properties;
 4 import java.util.Set;
 5 
 6 public class PropertiesDemo {
 7     public static void main(String[] args) {
 8         Properties properties = new Properties();
 9         properties.put("zhangsan", "23");
10         properties.put("lisi", "25");
11         properties.put("wangwu", "21");
12         properties.put("zhaoliu", "27");
13         Set<String> names = properties.stringPropertyNames();
14         //使用高级for遍历
15         for(String name : names) {
16             String value = properties.getProperty(name);
17             System.out.println(name + " = " + value);
18         }
19         //使用迭代器遍历
20         Iterator<String> iterable = names.iterator();
21         while(iterable.hasNext()){
22             String key = iterable.next();
23             String value = properties.getProperty(key);
24             System.out.println(key +" = " + value);
25         }
26         //使用枚举遍历
27         Enumeration<String> enumeration = (Enumeration<String>)properties.propertyNames();
28         while(enumeration.hasMoreElements()) {
29             String key = enumeration.nextElement();
30             String value = properties.getProperty(key);
31             System.out.println(key + " = " + value);
32         }
33     }
34 }