Configuration file Properties

Configuration file Properties

insert image 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 often changed. Allows the user to modify the relevant variable settings without the program itself. Just like in Java, its configuration file is often a .properties file, which is configured with parameters in the form of key-value pairs.

Implements all interfaces Serializable, Cloneable, Map<Object,​Object>
known direct subclasses: Provider

Constructor

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

Common method

getProperty(String key) Searches this property list for a property with the specified key. If the key is not found in this property list, the default property list and its default value are checked (recursively). If the property is not found, the method returns the default value parameter.
list(PrintStream out) Prints this property 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 is in the simple line-oriented format specified in Loader (Reader) and assumes the use of ISO 8859-1 character encoding; i.e. each byte is a Latin1 character. Characters not in Latin1 and some special characters are represented in keys and elements using Unicode escapes. After this method returns, the specified stream remains open.
setProperty(String key, String value) calls the Hashtable's method put . It sets the key-value pair by calling the put method of the base class.
store(OutputStream out, String comments) Writes this list of properties (key and element pairs) from this 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) Emits an XML document representing all attributes contained in this table using the specified encoding.
clear() clears 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, including a different key from the default property list if a key of the same name has not been found from the main property list. Properties with keys or keys 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 resulting style is as follows:
insert image description here
API: Api

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

insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324133094&siteId=291194637