Java project How to read the configuration file parameter information

Java reads the configuration file parameters:

Method a: to implement the configuration file read by the JDK Properties.

 

  Properties mainly for reading Java profiles, different programming languages ​​have their own supported profiles, profiles many variables are constantly changing, in order to facilitate the user's configuration, allowing the user to modify the program itself enough from the relevant variable settings. Parameter configuration like in Java, its configuration file often .properties file, in the form of key-value pairs.

1, the configuration file settings

StefanieSun sysName 
sysChinesName = Stefanie
sysBirthday: 1976-07-02
# spaces:, = three ways can indicate the presence of key-value pairs.

2, reads the new class

public  class systemProperties { 
  // set the configuration file path
  Private Final static String URLPATH1 = "CN / COM / Yitong / util / system.properties" ;
  Private urlPath2 Final static String = "the src / main / Java / CN / COM / Yitong / util /system.properties ";   
Private fianl static the Properties Properties = new new the Properties ();

  method 1: use classLoader relative to obtain directory file (file must be the same under the same directory SystemProperties; see path" FIG. 1 "; this file address is not required to precisely "src / main / java / cn / com / yitong / util / system.properties", because it is the same
      SystemProperties same directory)

  static {
    the try {
      the inputStream inputStream = ClassLoader.getSystemResourceAsStream (URLPATH1);
      Properties.load (inputStream);
      //
Properties.load (the InputStreamReader new new (ClassLoader.getSystemResourceAsStream (urlPath), "UTF-. 8")); a method similar to
    the catch} (IOExecption E) { 
      e.printStackTrace ();
    }  
  }

  Method 2: Use BufferedReader to read the configuration file. The configuration file may be read at an arbitrary path, not necessarily the same directory similar SystemProperties (This method reads the configuration file of any file, so the precise path relative path relative path [requires relatively precise way
     path to determine the file])
  {static
    the try {
      the BufferedReader BufferedReader the BufferedReader new new = (the FileReader new new (urlPath2));
      Properties.load (BufferedReader);
    } the catch (IOException E) {
      e.printStackTrace ();
    }
  }

  
  acquired value value method:
  
  public static String the getValue (String Key) {
    return Properties.getProperty (Key) .trim ();
  }
  
  // get the value through the key, if the return value is null defaultValue
  public static String getValue (String key, String defaultValue) {
    return properties.getProperty(key,defaultValue);
  }

}

figure 1

3, the test categories:

public class SystemPropertiesText {

    public static void main(String[] strings) {
      System.err.println(SystemProperties.getValue("sysName")); 
  }
}

Method two: by direct reading and the value ResourceBundle

  Method one reads the configuration file when the configuration supports multiple file formats (properties, md, etc.), and ResourceBundle can only read .properties format. ResourceBundle is mainly used to obtain documents internationalization, localization (details your own search to understand, but more explanation here).

1, profile information with a method

2, no specific method to write java class, may be obtained directly.

public class SystemPropertiesText{
  public static void main(String[] s){
    String urlPath = "
cn/com/yitong/util/system.properties";
    ResourceBundle resourceBundle
= ResourceBundle.getBundle(urlPath);     
    System.err.println(resourceBundle.getString(
"sysName"));
  }
}

Guess you like

Origin www.cnblogs.com/StefanieYang/p/12197951.html