其他分享
首页 > 其他分享> > Properties -IO相关的双列集合类

Properties -IO相关的双列集合类

作者:互联网

IO相关的集合类


    private static void show01() {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();
        //使用setProperties();添加数据
        properties.setProperty("No.1","王小帅");
        properties.setProperty("No.2","王一帅");
        properties.setProperty("No.3","王二帅");

        //使用stringPropertyNames把Properties集合中的key取出放入set集合
        Set<String> set = properties.stringPropertyNames();

        //遍历set集合,取出Properties集合的每一个键
        for (String key:set) {
            //通过getProperty方法通过key获取value
            String value = properties.getProperty(key);
            System.out.println(key + ":"+value);
        }

    }

store方法


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

public class Propertiess {
    public static void main(String[] args) {
        try {
            show01();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void show01() throws IOException {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();
        //使用setProperties();添加数据
        properties.setProperty("No.1","王小帅");
        properties.setProperty("No.2","王一帅");
        properties.setProperty("No.3","王二帅");

        //使用字符输出流,绑定输出目的地
        FileWriter writer = new FileWriter("b.txt");

        //使用Properties的方法store,把集合中的临时数据,持久化写入文件中
        properties.store(writer,"");
        /*
        # -comments注释类容
        #Thu Sep 26 18:21:37 CST 2019
        No.2=王一帅
        No.1=王小帅
        No.3=王二帅
         */

        //使用字节输出流
        properties.store(new FileOutputStream("a.txt"),"error");
        /*
        #error
        #Thu Sep 26 18:27:58 CST 2019
        No.2=\u738B\u4E00\u5E05
        No.1=\u738B\u5C0F\u5E05
        No.3=\u738B\u4E8C\u5E05
         */

        //关闭流
        writer.close();

    }

}

load方法

package cn.learn.properties;


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

public class Propertiess {
    public static void main(String[] args) {
        try {
            show01();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void show01() throws IOException {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();

        //使用字符输入流,绑定读取目的地
        FileReader reader = new FileReader("b.txt");

        //读取键值对进入集合,若有#开头的串不会被读取
        properties.load(reader);

        //遍历查看
        Set<String> strings = properties.stringPropertyNames();
        for (String key:strings) {
            System.out.println(key+":"+properties.getProperty(key));
            /*
            No.2:王一帅
            No.1:王小帅
            No.3:王二帅
             */
        }
        
        //释放
        reader.close();
    }

}

标签:java,IO,properties,双列,key,集合,Properties,String
来源: https://www.cnblogs.com/huxiaobai/p/11593666.html