Java method for reading properties configuration file

This article describes the method of reading the properties configuration file by Java. Share with you for your reference. The specific analysis is as follows:

In the past two days, I have been working on Java projects, using attribute files and searching for information on the Internet. For a long time, I haven't found a satisfactory way for me to read it The property values in the properties file are very depressed. There are probably the following methods to obtain the property values on the Internet. The following three methods are gradually optimized to achieve the best results. The following are all based on date Take the 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:

Although the above methods can also obtain the content of the configuration file, But the biggest problem is the location of the file path (that is, the value of filepath in the code). When absolute positioning is adopted, if the project is moved to another drive letter for operation, the source code needs to be modified, otherwise an error will be reported. However, if relative path is used, when stweek class is moved to another package, the source code still needs to be modified, otherwise an error will be reported. Therefore, this method is too limited and bad, so The second method can solve this problem, but it still has some problems

Method 2:

The advantage of this method is that there is no need to point out the absolute path of the configuration file. Whether you put the stweek class in another package or move the whole project to another drive letter, the code can still run normally without the problem of missing the file. However, this method still has a major defect, Because we often want the configuration file to be cached in memory, so we don't have to access the hard disk every time we read (accessing external memory is too time-consuming). Therefore, we want to use static variables and static methods to cache and obtain variables, and realize the dynamic load of these values. Then the problem comes 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 to do? Let's see the third method

Method 3:

This method can not only cache the contents of the configuration file, but also automatically load the contents of the configuration file into memory. Users do not need to consider the manual loading process at all, but only need to call stweek directly where needed Getstartdate() (because it is a static method, you don't even need to create an object in advance). In this way, if there is a cache in the memory, the function will directly read the data in the memory to save time. If there is no cache, you don't have to worry. The system will automatically load it for you. The user doesn't need to know how to implement it at all. Just need to know that I can directly call the function to obtain the desired value, huh Oh, keep it simple. Note: (it has nothing to do with the above, my own test)

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>