How to use JAVA to read and write Properties file?

Properties file is often visible in JAVA applications, and it is also a particularly important type of file. It is used to configure some information of the application, but these information are generally relatively small data, there is no need to use database files to save, but use general text files to save, if it is saved directly through File, it may be in the storage and It is not very convenient to read, but it is different if it is saved as a Properties file. The properties file has a key value corresponding to it. In the JAVA package, there is a class that provides a special operation property file. 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 manner. 

Note: The following code does not adopt the method of catching the exceptions thrown. When writing the program, you must pay attention to catching the exceptions. It is recommended to handle the caught exceptions. 

The Properties class inherits from the Hashtable class and adopts the storage method corresponding to the key value. What is the convenience of using the Properties class to manage property files? The Properties class has special read and write methods to read and write the Properties file. Don't worry about the format of reading and writing, just provide a read and write stream for the Properties class. Properties used to read and write properties files are: 
  

Java code
  1. //Method to read the property file stream    
  2.    public void load(InputStream inStream) throws IOException {}    
  3.    //Method of writing attribute file stream    
  4.    public void store(OutputStream out, String comments) throws IOException {}   


First, let's take a look at how to read properties from a properties file. 
Suppose we have created a new properties file, named prop.properties, with the following content: 
  

Java code
  1. sitename=abcjava    
  2.    siteurl = www.abcjava.com   


The first step we need to do is to read the file into the Properties class object, because load has a parameter of InputStream, so we can use the subclass of InputStream FileInputStream to read the properties file into the Properties object, knowing prop.properties Path, we use the FileInputStream(String name) constructor: 
  

Java code
  1. Properties prop =  new  Properties(); //Property collection object    
  2.    FileInputStream fis =  new  FileInputStream( "prop.properties" ); //Property file stream    
  3.    prop.load(fis); //Load the property file stream into the Properties object   



The next thing we will do is if we read an attribute, because each line in the attribute file corresponds to a key value, each line represents an attribute object, and each line will be stored in the relationship between the key and the value. In Properties, the Properties class provides the getProperty(String key) method to read the key value by the key name. When the key is not found in the property collection and you want to assign a value to the key in the program, you can use public String getProperty( String key, String defaultValue) method, this method means to search for attributes in the attribute list with the specified key. If the key is not found in the attribute list, then recursively check the default attribute list and its default value. If the attribute is not found, this method returns the default value variable: 
 

Java code
  1. //Get the attribute value, sitename has been defined in the file    
  2.    System.out.println( "Get property value: sitename="  + prop.getProperty( "sitename" ));    
  3.    //Get the attribute value, country is not defined in the file, a default value will be returned in this program, but the attribute file is not modified    
  4.    System.out.println( "Get property value: country="  + prop.getProperty( "country""China" ));   



After knowing how to read the property file, we have another very important thing to modify and add new properties to the property file, here is to use the public void store (OutputStream out, String comments) method, this method is to write the property collection To an OutputStream stream, the same as the InputStream stream, here is also the use of its subclass FileOutputStream (String name), not much to say here. 

Before saving the property collection to the file, one more thing we have is how to modify and add new properties to the property collection. Here we use a method called setProperty(String key, String value), which is when the specified property exists in the collection When the key is used, modify the value of the key, if it does not exist, create a new key, which is also saved through the key-value relationship, but it is worth noting that the Properties class inherits from Hashtable, so you can also use the put and putAll methods of Hashtable Save, 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, the setProperty method should be used. If the store or save method is called on a "dangerous" Properties object (that is, contains a key or value other than String), the call will fail. Well, let's take a look at the procedures for modifying, adding and saving properties: 

//Modify the property value of 
sitename prop.setProperty("sitename", "Boxcode"); 
//Add a new property studio 
prop.setProperty( "studio", "Boxcode Studio"); 
//File output stream 
A FileOutputStream fos = new new a FileOutputStream ( "prop.properties"); 
// save the stream into a set of Properties 
prop.store (fos, "Copyright (C) Boxcode Studio"); 
fos.close (); // close stream 

Continued It is the source code of the entire program: 
 

Java code
  1. import java.io.FileInputStream;   
  2. import java.io.FileOutputStream;   
  3. import java.util.Properties;   
  4.   
  5. public class PropertyEditor {   
  6.     public static void main(String[] args) throws Exception {   
  7.         Properties prop =  new  Properties(); // Property collection object   
  8.         FileInputStream fis =  new  FileInputStream( "prop.properties" ); // property file input stream  
  9.         prop.load(fis); // Load the property file stream into the Properties object   
  10.         fis.close(); // Close the stream   
  11.   
  12.         // Get the attribute value, sitename has been defined in the file   
  13.         System.out.println( "Get property value: sitename="  + prop.getProperty( "sitename" ));   
  14.         // 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   
  15.         System.out.println( "Get property value: country="  + prop.getProperty( "country""China" ));   
  16.   
  17.         // Modify the attribute value of sitename   
  18.         prop.setProperty("sitename""Boxcode");   
  19.         // Add a new attribute studio   
  20.         prop.setProperty("studio""Boxcode Studio");   
  21.         // File output stream   
  22.         FileOutputStream fos = new FileOutputStream("prop.properties");   
  23.         // Save the Properties collection to the stream   
  24.         prop.store(fos, "Copyright (c) Boxcode Studio");   
  25.         fos.close(); // Close the stream   
  26.     }   
  27. }  


After we know how to read and write a property file, we still have a lot of problems to pay attention to, because the load and store methods read and write the property stream file according to the ISO-8859-1 encoding, and the characters of ILatin1 and certain Some special characters, and for non-Latin1 characters and some special characters, you need to use escape sequences similar to those used for character and string literals to represent them in the form of values ​​and elements. So when we are dealing with Chinese, we can’t assign the Chinese value to the attribute when directly modifying the attribute file. Instead, we need to assign the Chinese value to the attribute through the setProperty method in the JAVA program, because the store will convert Chinese into Unicode code. When reading, the system will print the read unicode code according to the system code. For Chinese systems, it is usually GBK code so that Chinese can be displayed normally.

Guess you like

Origin blog.csdn.net/guoguo527/article/details/53241977