Properties 集合

  1. Map
    • Hashtable
      • Properties
  2. 特点:
    1. 该集合中的键和值都是字符串类型
    2. 集合中的数据可以保存到流中, 或者从流中获取
  3. 应用:
    • 通常该集合用于操作以键值对形式存在的配置文件
  4. 常用方法:
// 单个元素的存和取:
    // 存元素
        setProperty();
    // 取元素
        getProperty();

// 获取所有元素 (将 Map 集合转换成 Set 集合)
    Set<String> stringPropertyNames();  // 返回此属性列表中的键集

// 示例:
    public static void propertiesDemo(){

        // 创建一个 Properties 的集合
            Properties prop = new Properties();

        // 存储元素, 注意, 键和值都是字符串
            prop.setProperty("wangcai","22");
            prop.setProperty("lisi","32");
            prop.setProperty("zhangsan",35);

        // 修改元素, 键相同, 值覆盖
            prop.setProperty("lisi","14");

        // 获取所有元素
            Set<String> names = prop.stringPropertyNames();

            for(String name:names){
                String value = prop.getProperty(name);

                System.out.println(name+":"+value);
            }
    }

Properties 集合和流对象相结合的方法

// list(PrintStream out);
    public static void method(){

        Properties prop = new Properties();

        prop.setProperty("wangcai","18");
        prop.setProperty("xiaoqiang","28");
        prop.setProperty("zhaoliu","25");

        prop.list(System.out);  // 将集合信息打印到控制台
    }

// 将 Properties 表中的属性列表写入到输出流:
//        store(OutputStream out, String comments(属性列表的描述));
//        store(Writer writer, String comments);

public static void method_1(){

    Properties prop = new Properties();

    prop.setProperty("wangcai","18");
    prop.setProperty("xiaoqiang","28");
    prop.setProperty("zhaoliu","25");

    // 想要将这些集合中的字符串键值信息持久化存储到文件中
    // 需要关联输出流
    FileOutputStream fos = new FileOutputStream("info.txt");

    // 将集合中数据存储到文件中, 使用 store() 方法
    prop.store(fos,"name+age");

    fos.close(); // 关闭流
}

// 将硬盘上已有的文件信息读取到 Properties 集合中
//      load(InputStream in);  字节流
//       load(Reader reader);  字符流

    public static void method_2(){

        Properties prop = new Properties();

        // 集合中的数据来自与一个文件
        // 注意: 必须要保证该文件中的数据是键值对
        //  需要使用到读取流(字节流或字符流)
        FileInputStream fis = new FileInputStream("info.txt");

        // 使用 load() 方法
        prop.load();

        fis.close();
    }

    // 模拟实现 load() 方法
    public static void MyLoad(){
        Properties prop = new Properties();

        BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));

        String line = null;
        while((line=bufr.readLine())!=null){
            // 将 info.txt 中的注释文件过滤出去
            if(line.startsWith("#"))
                continue;

            // 使用 "=" 切割字符串
            String[] arr = line.split("=");

            // 存入 Properties 集合中
            prop.setProperty(arr[0],arr[1]);
        }

        bufr.close();
    }


// 示例二: 对已有的配置文件中的信息进行修改
        /*
         * 思路:
         *     读取这个文件
         *     将这个文件中的键值数据存储到集合中
         *     通过集合对数据进行修改
         *     在通过流将修改后的数据存储到文件中
         */

         public static void test() throws IOException{

            // 首先将文件封装成对象, 判断该文件是否存在
            // 如果不存在, 则创建一个新的
            File file = new File("info.txt");
            if(!file.exists()){
                file.createNewFile();
            }
            // 读取该文件, 流可以直接操作文件对象
            FileReader fr = new FileReader(file);

            // 将文件中的键值数据存储到集合中
            Properties prop = new Properties();
            prop.load(fr);

            // 修改数据
            prop.setProperty("zhaoliu","30");

            // 将修改后的 Properties 存储到文件中
            FileWriter fw = new FileWriter(file);
            prop.store(fw,"changeName");   // 这是将 info.txt 中的内容全部覆写一遍
                                           // 并不是仅仅修改了 zhaoliu
                                           // store() 方法需要两个参数
            fr.close();
            fw.close();

        }

参考资料

原文地址:https://www.cnblogs.com/linkworld/p/7517483.html