zoukankan      html  css  js  c++  java
  • Java遍历Properties的所有的元素

    Java遍历Properties的所有的元素 加载

    Properties properties = new Properties();

    properties.load(PlayGround.class.getClassLoader().getResourceAsStream("tag.properties"));

    输出

    /**
         * 输出properties的key和value
         */
        public static void printProp(Properties properties) {
            System.out.println("---------(方式一)------------");
            for (String key : properties.stringPropertyNames()) {
                System.out.println(key + "=" + properties.getProperty(key));
            }

            System.out.println("---------(方式二)------------");
            Set<Object> keys = properties.keySet();//返回属性key的集合
            for (Object key : keys) {
                System.out.println(key.toString() + "=" + properties.get(key));
            }

            System.out.println("---------(方式三)------------");
            Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }

            System.out.println("---------(方式四)------------");
            Enumeration<?> e = properties.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + "=" + value);
            }
        }

    复制代码
    /**
         * 输出properties的key和value
         */
        public static void printProp(Properties properties) {
            System.out.println("---------(方式一)------------");
            for (String key : properties.stringPropertyNames()) {
                System.out.println(key + "=" + properties.getProperty(key));
            }
    
            System.out.println("---------(方式二)------------");
            Set<Object> keys = properties.keySet();//返回属性key的集合
            for (Object key : keys) {
                System.out.println(key.toString() + "=" + properties.get(key));
            }
    
            System.out.println("---------(方式三)------------");
            Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }
    
            System.out.println("---------(方式四)------------");
            Enumeration<?> e = properties.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + "=" + value);
            }
        }
    复制代码
     
     
     
     
     
    jar包读取包内properties文件
     

    properties位于src目录下

    project

        --src

             -----package

             -----test.properties

    Properties p = new Properties();
    //从输入流中读取属性列表(键和元素对)
    p.load(DB.class.getClass().getResourceAsStream("/test.properties"));
    String testKey = p.getProperty("testKey");

    不能这样写

     File file = new File("src/test.properties");

    输出路径为会带!   因为是jar包

    java指定编码格式utf-8运行jar

    java -Dfile.encoding=utf-8 -jar test.jar
  • 相关阅读:
    Android Studio 开发
    Jsp编写的页面如何适应手机浏览器页面
    电影
    Oracle 拆分列为多行 Splitting string into multiple rows in Oracle
    sql server 2008 自动备份
    WINGIDE 激活失败
    python安装 错误 “User installations are disabled via policy on the machine”
    ble编程-外设发送数据到中心
    iOS开发-NSString去掉所有换行及空格
    ios9 字符串与UTF-8 互相转换
  • 原文地址:https://www.cnblogs.com/xinxihua/p/13294233.html
Copyright © 2011-2022 走看看