Configuration file Properties

Configuration file Properties

Insert picture description here

Properties (Java.util.Properties), this class is mainly used to read Java configuration files. Different programming languages ​​have their own supported configuration files. Many variables in the configuration files are frequently changed. In order to facilitate user configuration, Allow users to modify the related variable settings without the program itself. Just like in Java, its configuration file is often a .properties file, which configures parameters in the form of key-value pairs.

All interfaces implemented are Serializable, Cloneable, Map<Object,​Object>
known direct subclasses: Provider

Constructor

Properties() creates an empty list of properties without default values. Properties​(int initialCapacity)
creates an empty property list with no default value, and the initial size accommodates the specified number of elements without the need for dynamic resizing. Properties​(Properties
defaults) creates an empty property list with specified default values.

Common method

getProperty(String key) Search for the property with the specified key in this property list. If the key is not found in this attribute list, the default attribute list and its default value are checked (recursively). If the attribute is not found, the method returns the default value parameter.
list(PrintStream out) Print this attribute list to the specified output stream. This method is useful for debugging.
load(InputStream inStream) Reads a list of attributes (key and element pairs) from the input byte stream. The input stream adopts the simple line-oriented format specified in the Loader (Reader) and assumes the ISO 8859-1 character encoding; that is, each byte is a Latin1 character. Characters that are not in Latin1 and certain special characters are represented in keys and elements that use Unicode escape characters. After this method returns, the specified stream remains open.
setProperty(String key, String value) calls the put method of Hashtable. He sets the key-value pair by calling the put method of the base class.
store(OutputStream out, String comments) Write this property list (key and element pair) in the Properties table to the output stream in a format suitable for loading into the Properties table using the load (InputStream) method. This Properties method does not write out the properties (if any) in the defaults table of this Properties table.
storeToXML(OutputStream os, String comment, String encoding) Use the specified encoding to send out an XML document that represents all the attributes contained in this table.
clear() Clear this hash table so that it does not contain any keys.
stringPropertyNames() returns a set of keys in this property list, where the keys and their corresponding values ​​are strings. If a key with the same name has not been found from the main property list, the different keys in the default property list are included. Keys or properties whose keys are not of type String will be omitted

Specific implementation code:

package work.february.three;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

/**
 * @Author: 小浪
 * @Description:
 * @Date Created in 2021-02-03 14:48
 * @Modified By:
 */
public class Demo2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        /**
         * .Proprerties文件 以及 Proprerties类
         */
        Properties properties =new Properties();
        properties.put("name","哆啦A浪");
        properties.put("info","是个大帅哥");
        FileWriter fileWriter=new FileWriter("D://c.properties");
        properties.store(fileWriter,"人物简介");
        fileWriter.close();
        //读取
        Properties properties1 =new Properties();
        Reader reader =new FileReader("D://c.properties");
        properties.load(reader);
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.getProperty("info"));
        reader.close();




    }
}

The result style is as follows:
Insert picture description here
API: Api

When you see this, don't be stingy with the likes in your hand, give a free like and follow! If you have any questions, you can contact Xiaolang: [email protected], or private message.

Insert picture description here

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113606683