Talking about Properties Collection

1 Overview

java.util.PropertiesInherited from Hashtableto represent a persistent property set. It stores data using a key-value structure, where each key and its corresponding value is a string. This class is also used by many Java classes. For example, when obtaining system properties, System.getPropertiesthe method returns an Propertiesobject.

2 Properties class

Construction method

  • public Properties(): Creates an empty property list.

unique method 

method name

illustrate

Object setProperty​(String key, String value)

Set the key and value of the collection, both of which are of String type, and call the Hashtable method put at the bottom.

String getProperty​(String key)

Searches for properties using the keys specified in this property list.

Set<String> stringPropertyNames​()

Returns an unmodifiable set of keys from this property list, where the keys and their corresponding values ​​are strings.

Methods related to streams

method name

illustrate

void load​(InputStream inStream)

Reads a property list (key and element pair) from the input byte stream

void load​(Reader reader)

Reads a property list (key and element pair) from an input character stream

void store​(OutputStream out, String comments)

Writes this list of properties (key and element pairs) into this Properties table, in a format suitable for use with the load(InputStream) method to an output byte stream.

void store​(Writer writer, String comments)

Writes this list of properties (key and element pairs) into this Properties table, in a format suitable for use with the load(Reader) method to an output character stream.

  • public void load(InputStream inStream): Read key-value pairs from a byte input stream.

  • public void load(Reader reader): Read key-value pairs from a character input stream.

The byte input stream is used in the parameter. Through the stream object, it can be associated with a file, so that the data in the text can be loaded. Text data format:

filename=a.txt
length=209385038
location=D:\a.txt 

Load code demo:  

public static void main(String[] args) throws FileNotFoundException {
    // 创建属性集对象
    Properties pro = new Properties();
    // 加载文本中信息到属性集
    pro.load(new FileInputStream("read.txt"));
    // 遍历集合并打印
    Set<String> strings = pro.stringPropertyNames();
    for (String key : strings ) {
    	System.out.println(key+" -- "+pro.getProperty(key));
    }
}
  •  public void store(OutputStream outStream,String comments): save the key-value pairs of the Properties collection object to the file
  • public void store(Writer writer,String comments): Save the key-value pairs of the Properties collection object to the file
public static void main(String[] args) throws IOException {
        // 创建Properties集合对象
        Properties props = new Properties();

        // 创建InputStream/Reader的子类对象,绑定源文件
        InputStream is = new FileInputStream("day13_sw\\src\\config.properties");

        // Properties集合对象调用load方法传递InputStream/Reader的子类对象,
        // 把文件内容以键值对的方式加载到Properties集合对象中
        props.load(is);

        // 遍历集合
        Set<String> propertyNames = props.stringPropertyNames();

        for (String propertyName : propertyNames) {
            String propertyValue = props.getProperty(propertyName);
            if("age".equals(propertyName)) {//age属性的值增加10岁
                props.setProperty(propertyName,Integer.parseInt(propertyValue)+10+"");
            }
        }
        OutputStream os = new FileOutputStream("day13_sw\\src\\config.properties");
        // 把Properties集合对象中的内容存储到文件中
        props.store(os,null);
    }

 

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/131110110