Properties use Properties collections to store data, traversal, store,load methods

Keywords: Java encoding Attribute

package com.itheima.demo07.Prop;

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

/*

java.util.Properties collection extends Hashtable < k, V > implements Map < k, V >
The Properties class represents a persistent set of attributes. Properties can be stored in or loaded from a stream.
Properties set is a unique set that combines with IO flow
    You can use the method store in the Properties collection to write temporary data in the collection and persist it to the hard disk for storage.
    You can use the method load in the Properties collection to read the files (key-value pairs) saved in the hard disk into the collection for use.

Each key and its corresponding value in the property list is a string.
    Properties collection is a double-column collection, key and value are strings by default

*/
public class Demo01Properties {

public static void main(String[] args) throws IOException {
    show03();
}

/*
    You can use the method load in the Properties collection to read the files (key-value pairs) saved in the hard disk into the collection for use.
    void load(InputStream inStream)
    void load(Reader reader)
    Parameters:
        InputStream inStream:Byte input stream, unable to read Chinese key value pairs.
        Reader reader:Character input stream, which can read key pairs in Chinese.
    Use steps:
        1.Create Properties Collection Objects
        2.Use the method load in the Properties collection object to read files that hold key-value pairs
        3.Traversing Properties Sets
    Be careful:
        1.In files that store key-value pairs, the default connection symbols for keys and values can be used with =, spaces (other symbols)
        2.In a file that stores key-value pairs, you can use # to annotate, and the annotated key-value pairs will not be read again.
        3.In files storing key-value pairs, keys and values are string by default, without quotation marks.
 */
private static void show03() throws IOException {
    //1. Create Properties Collection Objects
    Properties prop = new Properties();
    //2. Use the method load in the Properties collection object to read files that hold key-value pairs
    prop.load(new FileReader("09_IOAndProperties\\prop.txt"));
    //prop.load(new FileInputStream("09_IOAndProperties\\prop.txt"));
    //3. Traversing Properties Sets
    Set<String> set = prop.stringPropertyNames();
    for (String key : set) {
        String value = prop.getProperty(key);
        System.out.println(key+"="+value);
    }
}

/*
    You can use the method store in the Properties collection to write temporary data in the collection and persist it to the hard disk for storage.
    void store(OutputStream out, String comments)
    void store(Writer writer, String comments)
    Parameters:
        OutputStream out:Byte output stream cannot be written in Chinese
        Writer writer:Character output stream, which can be written in Chinese.
        String comments:Annotations are used to explain what the saved files are for.
                If you can't use Chinese, it will generate garbled, and the default is Unicode encoding.
                Usually empty strings are used

    Use steps:
        1.Create Properties collection objects and add data
        2.Create byte output stream/character output stream objects and bind the destination to be output in the constructor
        3.Using the method store in the Properties collection, temporary data in the collection is persisted and written to the hard disk for storage.
        4.Release resources
 */
private static void show02() throws IOException {
    //1. Create Properties collection objects and add data
    Properties prop = new Properties();
    prop.setProperty("Xie","168");
    prop.setProperty("Di Ali Gerba","165");
    prop.setProperty("Guli Nazha","160");

    //2. Create byte output stream/character output stream objects and bind the destination to be output in the constructor
    //FileWriter fw = new FileWriter("09_IOAndProperties\\prop.txt");

    //3. Use the method store in the Properties collection to write the temporary data in the collection to the hard disk for storage.
    //prop.store(fw,"save data");

    //4. Releasing resources
    //fw.close();

    prop.store(new FileOutputStream("09_IOAndProperties\\prop2.txt"),"");
}

/*
    Store data using Properties collections and traverse to retrieve data from Properties collections
    Properties A collection is a double-column collection, key s and value s are strings by default.
    Properties Collections have some unique methods of manipulating strings
        Object setProperty(String key, String value) Call the Hashtable method put.
        String getProperty(String key) Finding value by key is equivalent to get(key) method in Map set.
        Set<String> stringPropertyNames() Returns the key set in this attribute list, where the key and its corresponding value are strings, which correspond to the keySet method in the Map set.
 */
private static void show01() {
    //Create Properties Collection Objects
    Properties prop = new Properties();
    //Add data to the collection using setProperty
    prop.setProperty("Xie","168");
    prop.setProperty("Di Ali Gerba","165");
    prop.setProperty("Guli Nazha","160");
    //prop.put(1,true);

    //Use string Property Names to remove keys from the Properties collection and store them in a Set collection
    Set<String> set = prop.stringPropertyNames();

    //Traverse the Set collection and take out each key of the Properties collection
    for (String key : set) {
        //Get value by key using getProperty method
        String value = prop.getProperty(key);
        System.out.println(key+"="+value);
    }
}

}

Posted by Az_Critter on Tue, 01 Oct 2019 21:24:46 -0700