Home  >  Article  >  Java  >  How to read properties configuration file in java

How to read properties configuration file in java

高洛峰
高洛峰Original
2017-01-12 10:36:091456browse

The example of this article describes the method of java reading the properties configuration file. Share it with everyone for your reference. The specific analysis is as follows:

In the past two days, I have been working on java projects and used property files. I searched the Internet for information. For a long time, I could not find a satisfactory method that allowed me to read the property values ​​​​in the .properties file. It was very difficult. It’s depressing. There are roughly the following methods for obtaining attribute values ​​​​on the Internet. The following three methods are gradually optimized to achieve the best results. The following uses the date.properties file as an example. The file is placed in the src directory. The file content is:

startdate=2011-02-07
totalweek=25

Method 1:

public class Stweek {
 static private String startdate = null;
 static private String totalweek = null;
 synchronized static public void loads(){
  if(startdate == null || totalweek == null)
  {
   FileInputStream is = null;
   Properties dbProps = new Properties();
   try {
    is = new FileInputStream(filepath); 
    dbProps.load(is);
    startdate = dbProps.getProperty("startdate");
    totalweek = dbProps.getProperty("totalweek");
   }
   catch (Exception e) {
    System.err.println("不能读取属性文件. " +
    "请确保db.properties在CLASSPATH指定的路径中");
   }
  }
 }
 public static String getStartdate() {
  if(tartdate==null)
   loads();
   return startdate;
 }
 public static String getTotalweek() {
  if(startdate==null)
   loads();
   return totalweek;
 }
}

Although the above method can also obtain the configuration file content, its biggest problem is The positioning of the file path (that is, the filepath value problem in the code), when using absolute positioning, if you move the project to another drive letter to run, you need to modify the source code, otherwise an error will be reported, but if you use a relative path , when the Stweek class is moved to another package, the source code still needs to be modified, otherwise an error will be reported, so this method is too limited and not good. The following method 2 can solve this problem, but it still has some problems

Method 2:

public class Stweek {
 InputStream is = null;
 Properties dbProps = null;
 public Stweek() {
  // TODO Auto-generated constructor stub
  is = getClass().getResourceAsStream("/date.properties");
  dbProps = new Properties();
  try {
   dbProps.load(is);
  }
  catch (Exception e) {
   System.err.println("不能读取属性文件. " +
   "请确保db.properties在CLASSPATH指定的路径中");
  }
 }
 public String getStartdate()
 {
  String sd = null;
  sd = dbProps.getProperty("startdate");
  return sd;
 }
 public String getTotalweek()
 {
  String totalweek=null;
  totalweek = dbProps.getProperty("totalweek");
  return totalweek;
 }
}

The advantage of this method is that you don’t need to point out the absolute path of the configuration file, and whether you put the Stweek class into another package or move the entire project to another drive letter , the code can still run normally, and there will be no problem of not finding the file. However, this method still has a major flaw, because we often hope that the configuration file can be cached in memory, so that we do not have to access the hard disk every time it is read. (Accessing external memory is too time-consuming). For this reason, we hope to use static variables and static methods to cache and obtain variables, and at the same time be able to dynamically load these values. Then the problem arises, because getClass().getResourceAsStream(" /date.properties"); This sentence can only appear in the constructor (you can test it yourself). This method cannot be used in dynamic load. What should I do? Let’s look at the third method

Method three:

import java.io.InputStream;
import java.util.Properties;
public class Stweek {
 static private String startdate = null;
 static private String totalweek = null;
 static{
  loads();
 }
 synchronized static public void loads(){
  if(startdate == null || totalweek == null)
  {
   InputStream is = Stweek.class.getResourceAsStream("/date.properties");
   Properties dbProps = new Properties();
   try {
    dbProps.load(is);
    startdate = dbProps.getProperty("startdate");
    totalweek = dbProps.getProperty("totalweek");
   }
   catch (Exception e) {
    System.err.println("不能读取属性文件. " +
    "请确保db.properties在CLASSPATH指定的路径中");
   }
  }
 }
 public static String getStartdate() {
 if(startdate==null)
  loads();
  return startdate;
 }
 public static String getTotalweek() {
 if(startdate==null)
 loads();
 return totalweek;
 }
}

This method can not only cache the contents of the configuration file, but also automatically load the contents of the configuration file into the memory. The user does not need to consider the manual loading process at all, and only needs to use it where it is needed. Just call Stweek.getStartdate() directly (because it is a static method, you don’t even need to create the object in advance). In this way, if there is a cache in the memory, the function will directly read the data in the memory, saving time. If there is no cache, Don't worry, the system will automatically load it for you. Users don't need to know how it is implemented. They only need to know that I can directly call the function to get the desired value.

I hope this article will help everyone's Java Programming helps.

For more articles on how to read properties configuration files in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn