特殊操作流之properties

public class Properties
extends Hashtable<Object,Object>
Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。
All Implemented Interfaces:
SerializableCloneableMap<Object,Object>
properties作为Map集合使用
package com.io.liushuaishuai;
/*
Properties作为Map集合的使用
 */

import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
    public static void main(String[] args) {
        Properties prop = new Properties();
        prop.put("fei01", "林青霞");
        prop.put("fei02", "悟空");
        prop.put("fei03", "八戒");

//        遍历集合
        Set<Object> keyset = prop.keySet();
        for (Object s : keyset) {
            System.out.println(s + " " + prop.get(s));
        }
    }

}
package com.io.liushuaishuai;
/*
Properties作为Map集合的使用
 */

import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
    public static void main(String[] args) {
        Properties prop = new Properties();
        prop.setProperty("fei01", "林青霞");
        prop.setProperty("fei02", "悟空");
        prop.setProperty("fei03", "八戒");
        System.out.println(prop);

//        遍历集合
        Set<Object> keyset = prop.keySet();
        for (Object s : keyset) {
            System.out.println(s + " " + prop.getProperty((String) s));
        }
    }

}

 

package com.io.liushuaishuai;
/*
Properties作为Map集合的使用
 */

import java.io.FileReader;

import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        /*

        prop.setProperty("fei01", "林青霞");
        prop.setProperty("fei02", "悟空");
        prop.setProperty("fei03", "八戒");
        */
        //System.out.println(prop);


        //将集合中的数据保存到文件
        //创建输出流
        /*
        FileWriter fw = new FileWriter("myIOstream\fos.txt");
        prop.store(fw,null);
        fw.close();
        */


        //将文件中的数据加载到集合中
//        创建输入流
        FileReader fr = new FileReader("myIOstream\fos.txt");
        prop.load(fr);


//        遍历集合
        Set<Object> keyset = prop.keySet();
        for (Object s : keyset) {
            System.out.println(s + " " + prop.getProperty((String) s));
        }

    }

}
原文地址:https://www.cnblogs.com/lsswudi/p/11431740.html