The use of Properties class in java

1. What is Properties?

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. For the convenience of user configuration, you can Allow users to modify 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.

2. Commonly used methods

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 attribute 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 property list (key and element pair) from the input byte stream. The input stream is in the simple line-oriented format specified in Load (Reader), and is assumed to use ISO 8859-1 character encoding; that is, each byte is a Latin1 character. Characters not in Latin1 and certain 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 method put of Hashtable. He sets the key-value pair by calling the put method of the base class.

store(OutputStream out, String comments) writes   this property list (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 properties (if any) in this Properties table's defaults 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 hashtable so that it contains no keys.

stringPropertyNames()  returns the set of keys in this property list where the keys and their corresponding values ​​are strings, including a different key in 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.

 

Guess you like

Origin blog.csdn.net/young_man2/article/details/126718494