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

posted on 2020-07-13 17:03  shuzihua  阅读(1785)  评论(0编辑  收藏  举报

导航