Java gets a sample properties properties file

  • 2020-04-01 02:42:09
  • OfStack

One property list can contain another property list as its "default"; 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. These two methods are not recommended because they allow the caller to insert an item whose key or value is not a String. Instead, use the setProperty method. If you call the store or save method on an "unsafe" Properties object (that is, a key or value that contains a non-string), the call fails. Similarly, if you call the propertyNames or list method on an "unsafe" Properties object (that is, a key that contains a non-string), the call fails.

Properties files are often visible in JAVA applications and are an especially important class of files. It is used to configure the application of some information, but the information is generally less data, there is no need to use to save the database File, and use general text files to save, if it is through the File save directly, it may be in storage and read is not very convenient, but if saved as a Properties File is different, have keys corresponding Properties File, in the JAVA package, offer special operation Properties File. This class is the java.uitl.properties class, and since the Properties class is a collection class, Properties reads and writes Properties as a collection.

Note: the following code for the exception is not taken to capture the way, when you write the program must pay attention to catch the exception, it is recommended to catch the exception.

The Properties class inherits the Hashtable class and USES the storage method corresponding to the key value. How convenient is it to use the Properties class to manage the Properties file? The Properties class has special read-write methods to read and write to the Properties Properties file, so don't worry about the format of the read and write, just provide a read and write flow to the Properties class. Properties is used to read and write Properties files by:


//Method to read the properties file stream

public void load(InputStream inStream) throws IOException {} 

//Method to write a stream of properties files

public void store(OutputStream out, String comments) throws IOException {} 

First, let's look at how to read properties from a properties file.

Suppose we have created a new properties file named prop.properties, which reads as follows:


sitename=abcjava
siteurl=www.abcjava.com

The first thing we need to do is to read the file into the Properties class object. Since the load has an InputStream parameter, we can use the subclass of InputStream, FileInputStream, to read the Properties file into the Properties object.


Properties prop = new Properties();//Property collection object

FileInputStream fis = new FileInputStream("prop.properties");//Properties file stream

prop.load(fis);//Load the Properties file stream into the Properties object

After know how to read the properties file we have a very important thing is to modify and add new attributes to the file, here is the use of public void store (OutputStream out, String comments) method, this method is to attribute set wrote an OutputStream stream, like InputStream flow, here also is to use its subclasses FileOutputStream (String name), there is not much.

Before save the property set to the file, we still have one thing is how to modify and add new attributes to the collection, and USES a method here is setProperty (String key, String value), the method is specified in the attribute set is the key, just change the key value, if not, will create a new key, also is saved by the key value relationship, but it is worth noting that the Properties since the Hashtable class hierarchy, So the put and putAll methods of Hashtable can also be saved, but they are strongly discouraged because they allow the caller to insert items whose keys or values are not Strings. Instead, use the setProperty method. If you call the store or save method on a "dangerous" Properties object (that is, a key or value that contains a non-string), the call fails. Well, let's take a look at the programs that modify, add, and save properties:


//Modify the property value of sitename

prop.setProperty("sitename", "Boxcode"); 

//Add a new property 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

Next is the entire program source code:


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 Properties file stream into the Properties object

fis.close();//Close the stream

//Gets the value of the property that sitename has defined in the file

System.out.println(" Get the property value: sitename=" + prop.getProperty("sitename")); 

//Gets the property value, country not defined in the file, returns a default value in this program, but does not modify the properties file

System.out.println(" Get the property value: country=" + prop.getProperty("country", " China ")); 

//Modify the property value of sitename

prop.setProperty("sitename", "Boxcode"); 

//Add a new property 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

}  
} 


Related articles: