特殊集合之Properties类

特殊集合之Properties类

java.util public class Properties
extends java.util.Hashtable<Object, Object>
  • properties 在 java.util 包中
  • 是HashTable的子类 是一个map类型的集合
  • 读取文件中的信息------》流(高级流)

Properties类表示一个持久的属性集,存储的都是属性。
Properties可以保存到流中或从流中加载(常与流结合使用)。
Properties属性列表中的每个键及其对应的值都是字符串类型(所以该类没有泛型)。

Properties常用方法

// 1、创建一个Properties集合对象
	Properties properties = new Properties();

// 2、向Properties中添加数据,以键值对形式添加
	properties.setProperty(String key, String value);

// 3、修改指定键所对应的值 当key不存在时,则为添加方法 
//    当key存在,则该方法为修改,返回值是之前key对应的value
	Object o = properties.setProperty("key","新value")// 4、通过 key 键,获取 key 所对应的 value值
	String value = properties.getProperty(key);

// 5、通过 stringPropertyNames()  方法获得 键集合
	Set<String> keys  = properties.stringPropertyNames();

Properties类常用方法演示

Public class Test_Properties {
	public static void main(String[] args){

        // 创建一个Properties集合类对象
        Properties properties = new Properties();

        // 向Properties中添加数据,以键值对形式添加
        properties.setProperty("name", "张三");
        properties.setProperty("age", "20");
        properties.setProperty("sex", "男");

        // 通过 stringPropertyNames() 方法获得 键集合
        Set<String> keys = properties.stringPropertyNames();

        // 循环遍历集合
        for (String key : keys) {
            System.out.println(key+"="+properties.get(key));
        }

        // 通过迭代器进行迭代遍历
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            // 通过键(key)获取值(value)
            String value = properties.getProperty(key);
            System.out.println(key+"-"+value);
        }

        // 修改某个键所对应得值 继续添加 当键与集合中得重复 则就式修改了
        Object o = properties.setProperty("name", "李四");
        // 返回值 是之前 key 对应的 value
        System.out.println(o.toString());// 旧value

        Iterator<String> iterator2 = keys.iterator();
        while (iterator2.hasNext()){
            String key = iterator2.next();
            String value = properties.getProperty(key);
            System.out.println(key+"-"+value);
        }
    }
}

Properties系统属性集

System.getProperty(String)方法使用的当前系统属性集作为Properties对象返回。 如果没有当前的系统属性集,则首先创建和初始化一组系统属性。

// 确定得到当前系统的属性集
public static Properties getProperties();

获取系统属性集代码示例

public class TestSystemProperties2 {
    public static void main(String[] args) {

        // 通过 System类 获取系统的属性,并存储到集合中
        Properties systemProperties = System.getProperties();

        // 通过迭代器方式进行迭代
        Set<String> names = systemProperties.stringPropertyNames();
        Iterator<String> iterator = names.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = systemProperties.getProperty(key);
            System.out.println(key+"="+value);
        }
        
        // 将系统的信息集输出到控制台
        systemProperties.list(System.out);
    }
}

Properties类与配置文件

  • 使用集合与输入输出流来操作配置文件,是开发中常用到的事儿
  • 并且配置文件中的存储格式为:key=value(并独占一整行)
  • Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式
  • Properties类表示一组持久的属性。Properties`可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。

主要有以下几个步骤

  1. 读取配置文件信息放入Properties集合中
  2. 修改Properties集合中的信息
  3. 将集合的信息写到配置文件中
  • 涉及到的方法
方法描述
void load(InputStream inStream)从输入字节流读取属性列表(键和元素对)。
void store(OutputStream out, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。
  • 具体操作步骤
public class TestProperties3 {
    public static void main(String[] args) {
        try {
            System.out.println("properties集合创建成功");
            // 在内存中创建集合
            Properties properties = new Properties();

            // 创建字符读取流
            FileInputStream fis= new FileInputStream("URL\config.properties");

            // 将文件中的键值对存储到 properties 集合里
            properties.load(fis);
            // properties 执行之后,集合里面就已经装好文件中的内容了

            // 修改 name的值
            properties.setProperty("name", "郑子文");
            System.out.println("修改集合中的值");

            // 将内存中做的修改 写回配置文件中
            FileWriter fileWriter = new FileWriter("URL\config.properties");

            // store() 方法将properties集合在内存中修改后的值 写入文件中
            // store() 的第二个参数是描述该文件的信息
            properties.store(fileWriter, "注释");

            System.out.println("将修改后的集合重新写入集合中");

            // 关闭
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/HW-CJY/p/13751684.html