当前位置:  首页>> 技术小册>> Java语言基础10-Java中的集合

● Properties 类是 Hashtable 的子类,该对象是用来处理属性文件的。
● 由于属性文件的 key 和 value 都是字符串类型,所以 Properties 里的 key 和 value 都是字符串类型。
● 存取数据的时候,建议使用 setPropert y和 getProperty 方法。

● 示例

  1. package com.github.map.demo9;
  2. import java.util.Enumeration;
  3. import java.util.Properties;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. */
  8. public class Test {
  9. public static void main(String[] args) {
  10. Properties properties = new Properties();
  11. properties.setProperty("hello", "你好");
  12. properties.setProperty("world", "世界");
  13. String hello = properties.getProperty("hello");
  14. System.out.println("hello = " + hello);
  15. String world = properties.getProperty("world");
  16. System.out.println("world = " + world);
  17. Enumeration<?> enumeration = properties.propertyNames();
  18. while (enumeration.hasMoreElements()) {
  19. Object key = enumeration.nextElement();
  20. Object value = properties.get(key);
  21. System.out.println("key = " + key + ",value = " + value);
  22. }
  23. for (String name : properties.stringPropertyNames()) {
  24. String property = properties.getProperty(name);
  25. System.out.println("key = " + name + ",value = " + property);
  26. }
  27. }
  28. }

该分类下的相关小册推荐: