Java operation Properties file introduction, javaproperties

Source: Internet
Author: User

Java operation Properties file introduction, javaproperties
Introduction to the Properties file for Java operationsOverview

During Java programming, we often need to create some configuration files inside or outside the project to maintain some basic configuration related to the project.

Therefore, we often need to read and write these configuration files. This chapter describes several common methods for operating Properties files.

 

Properties File Operations

The Properties file is one of the most frequently used configuration file types in our daily development. For this type of file, we can use the following methods to read/write it:

  • Use the built-in java. util. ResourceBundle class or java. util. PropertyResourceBundle class to operate the Properties file;
  • Use the built-in java. util. Properties class to operate the Properties file;
  • Use Commons-Configuration of Apache to operate the Properties File

 

ResourceBundle operation Properties File

Since the PropertyResourceBundle class inherits from ResourceBundle, the operations on the Properties file are very similar. Therefore, we will only start with the ResourceBundle class to briefly introduce its usage of operating the Properties file.

The operation method can be found in the following code:

 1 package com.webopenshare.demo.prop.utils; 2  3 import java.util.MissingResourceException; 4 import java.util.ResourceBundle; 5  6 public class PropUtils { 7      8     private static PropUtils _instance = null; 9     10     private ResourceBundle resBundle = null;11     12     static {13         _instance = new PropUtils();14     }15     16     private PropUtils() {17         loadSysConfig("sysConfig");18     }19     20     public static PropUtils getInstance() {21         return _instance;22     }23     24     public ResourceBundle loadSysConfig(final String fileName) {25         resBundle = ResourceBundle.getBundle(fileName);26         return resBundle;27     }28     29     public String getSysConfig(final String key) {30         String sysConfig = null;31         32         try {33             sysConfig = resBundle.getString(key);34         } catch (MissingResourceException e) {35             sysConfig = key;36         }37         38         return sysConfig;39     }40 }
ResourceBundle operation Properties sample code

Through the above code, we can see that the ResourceBundle class can easily read the configuration content of the Properties file, but what if we want to modify the configuration content? Unfortunately, by viewing the ResourceBundle class, we can see that it does not provide any method for this operation.

 

Java. util. Properties

Java comes with the java. util. Properties class for reading and writing Properties files, as shown in the following code:

 1 package com.webopenshare.demo.prop.utils; 2  3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.Properties; 6  7 public class PropUtils { 8      9     private static PropUtils _instance = null;10     11     private Properties props = null;12     13     private final String CONFIG_FILE = "/sysConfig.properties";14     15     static {16         _instance = new PropUtils();17     }18     19     private PropUtils() {20         loadSysConfig(CONFIG_FILE);21     }22     23     public static PropUtils getInstance() {24         return _instance;25     }26     27     public Properties loadSysConfig(final String fileName) {28         props = new Properties();29         try {30             props.load(Properties.class.getResourceAsStream(fileName));31         } catch (IOException e) {32             e.printStackTrace();33         }34         return props;35     }36     37     public String getSysConfig(final String key) {38         String sysConfig = props.getProperty(key, key);39         return sysConfig;40     }41     42     public void updateSysConfig(final String key, final String value) {43         props.setProperty(key, value);44     }45     46     public void persistSysConfig() {47         try {48             props.store(new FileOutputStream(System.getProperty("user.dir") + "/config" + CONFIG_FILE), "Updated System Configure");49         } catch (IOException e) {50             e.printStackTrace();51         }52     }53 }
Java. util. Properties: code example for reading and writing the Propery configuration file

Through the above example, we know:

  • You can use the load (inStream) method of java. util. Properties to load the configuration of the Properties file;
  • You can use the getProperty (key) or getProperty (key, defaultValue) method to obtain the Configuration Attribute of the specified key;
  • You can use setperty (key, value) to insert or follow the new configuration;
  • Through the store (outStream, comment) method, the final modification can be persisted into the file.

However, java. util. when Properties and new or new configuration Properties are inserted, we cannot retain the configuration format of the original file, including the configuration order and configuration Comments. This is not the best effect, we hope to maintain the configured Comments, which will be important for subsequent maintenance.

 

Apache Commons Configure operation Properties File

We can use the open-source Apache project and Commons-Configuration to operate the Properties file. The following dependencies need to be introduced during use:

  • Commons-configuration2-$ {releaseVersion}. jar
  • Commons-lang3-$ {releaseVersion}. jar
  • Commons-logging-$ {releaseVersion}. jar
 1 package com.webopenshare.demo.prop.utils; 2  3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8  9 import org.apache.commons.configuration2.PropertiesConfiguration;10 import org.apache.commons.configuration2.PropertiesConfigurationLayout;11 import org.apache.commons.configuration2.ex.ConfigurationException;12 13 public class PropUtils {14     15     private static PropUtils _instance = null;16     17     private static PropertiesConfiguration config = new PropertiesConfiguration();18     19     private static PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();20     21     private static final String CONFIG_FILE = "C:/Config/sysConfig.properties";22     23     static {24         _instance = new PropUtils();25         _instance.loadSysConfig(CONFIG_FILE);26     }27     28     private PropUtils() {29     }30     31     public static PropUtils getInstance() {32         return _instance;33     }34     35     public void loadSysConfig(final String fileName) {36         try {37             layout.load(config, new InputStreamReader(new FileInputStream(fileName)));38         } catch (FileNotFoundException | ConfigurationException e) {39             e.printStackTrace();40         }41     }42     43     public static String getStringConfig(final String key) {44         return config.getString(key);45     }46     47     public static void addOrUpdateStringConfig(final String key, final String value) {48         config.setProperty(key, value);49         persistSysConfg();50     }51     52     public static void persistSysConfg() {53         try {54             FileWriter out = new FileWriter(CONFIG_FILE, false);55             layout.save(config, out);56         } catch (IOException | ConfigurationException e) {57             e.printStackTrace();58         }59     }60 }
Apache Commons Configure operation Properties code example

With this open-source library, we can easily maintain the configuration file information, and ensure that when new or new configuration information is followed, the file format is not affected (the configuration order and Comments are retained ).

 

 

I want to subscribe to sponsors and encourage the author to write better articles:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.