A property list can contain another property list as its "default value"; if the property key cannot be found in the original property list, the second property list is searched.
Because Properties inherit from Hashtable, the put and putAll methods can be applied to the Properties object. However, these two methods are not recommended because they allow the caller to insert items whose keys or values are not Strings. Instead, use the setProperty method. If the store or save method is called on an "unsafe" Properties object (that is, containing a non-String key or value), the call will fail. Similarly, if the propertyNames or list method is called on an "unsafe" Properties object (that is, containing a non-String key), the call will fail.
Properties files are often visible in JAVA applications and are also a particularly important type of file. It is used to configure some information of the application, but this information is generally relatively small data. There is no need to use a database file to save it, but use a general text file to save it. If it is saved directly through File, it may be stored and saved. It is not very convenient to read, but it is different if it is saved as a Properties file. The property files have corresponding key values. In the JAVA package, there are special classes for operating property files. This class is the java.uitl.Properties class. Since the Properties class is a collection class, Properties will read and write properties in a collection.
Note: The following code does not capture the exceptions thrown. When writing the program, you must pay attention to catching exceptions. It is recommended to handle the captured exceptions.
The Properties class inherits the Hashtable class and adopts a storage method corresponding to key values. What is the convenience when using the Properties class to manage property files? The Properties class has special reading and writing methods to read and write Properties attribute files. You don't have to worry about the format of reading and writing. You only need to provide a reading and writing stream for the Properties class. Properties methods used to read and write properties files are:
Copy the code code as follows:
//Method to read attribute file stream
public void load(InputStream inStream) throws IOException {}
//Method to write attribute file stream
public void store(OutputStream out, String comments) throws IOException {}
First, let's look at how to read properties from a properties file.
Assume that we have created a new properties file named prop.properties with the following content:
Copy the code code as follows:
sitename=abcjava
siteurl=www.abcjava.com
The first step we need to do is to read the file into the Properties class object. Since one parameter of load is InputStream, we can use the InputStream subclass FileInputStream to read the property file into the Properties object. Know prop.properties path, we use the FileInputStream(String name) constructor:
Copy the code code as follows:
Properties prop = new Properties();//Property collection object
FileInputStream fis = new FileInputStream("prop.properties");//Properties file stream
prop.load(fis);//Load the property file stream into the Properties object
After knowing how to read the properties file, we have another very important thing to do, which is to modify and add new properties to the properties file. Here we use the public void store (OutputStream out, String comments) method. This method writes the property collection To an OutputStream stream, just like the InputStream stream, its subclass FileOutputStream (String name) is also used here, so I won’t go into details here.
Before saving the property collection to the file, we have one more thing to do is how to modify and add new properties to the property collection. One method used here is setProperty(String key, String value), this method is to modify the value of the key when the specified key exists in the attribute collection. If it does not exist, create a new key. It is also saved through the key-value relationship, but it is worth noting that the Properties class inherits Since Hashtable, it is also possible to save using Hashtable's put and putAll methods, but the use of these two methods is strongly discouraged because they allow the caller to insert items whose keys or values are not Strings. Instead, use the setProperty method. If the store or save method is called on a Properties object that is "at risk" (that is, contains a non-String key or value), the call will fail. Well, let's take a look at the procedures for modifying, adding and saving attributes:
Copy the code code as follows:
//Modify the attribute value of sitename
prop.setProperty("sitename", "Boxcode");
//Add a new attribute studio
prop.setProperty("studio", "Boxcode Studio");
//File output stream
FileOutputStream fos = new FileOutputStream("prop.properties");
//Save the Properties collection into the stream
prop.store(fos, "Copyright (c) Boxcode Studio");
fos.close();//Close the stream
Next is the source code of the entire program:
Copy the code code as follows:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyEditor {
public static void main(String[] args) throws Exception {
Properties prop = new Properties(); // Property collection object
FileInputStream fis = new FileInputStream("prop.properties");//Properties file input stream
prop.load(fis);//Load the property file stream into the Properties object
fis.close(); // Close the stream
// Get the attribute value, sitename has been defined in the file
System.out.println("Get property value: sitename=" + prop.getProperty("sitename"));
// Get the attribute value. Country is not defined in the file. A default value will be returned in this program, but the attribute file will not be modified.
System.out.println("Get property value: country=" + prop.getProperty("country", "China"));
// Modify the attribute value of sitename
prop.setProperty("sitename", "Boxcode");
//Add a new attribute studio
prop.setProperty("studio", "Boxcode Studio");
//File output stream
FileOutputStream fos = new FileOutputStream("prop.properties");
// Save the Properties collection to the stream
prop.store(fos, "Copyright (c) Boxcode Studio");
fos.close();//Close the stream
}
}