zoukankan      html  css  js  c++  java
  • 遍历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
    ---------------------------------

  • 相关阅读:
    闭包和this
    闭包与变量
    闭包
    ES6扩展运算符的几个小技巧
    js对象的深拷贝
    js获取当前点击元素的索引
    前端学习指北
    css实现心形图案
    this 知多少
    js数字进制转换
  • 原文地址:https://www.cnblogs.com/daniell003/p/3378106.html
Copyright © 2011-2022 走看看