The example of this article tells the method of Java read the Properties configuration file. Share it for everyone for your reference. The specific analysis is as follows:
In the past two days, doing the Java project, using attribute files, and checking information online. I did n’t find a satisfactory way to let me read the attribute value in the .properties file. There are the following methods. The following three methods are gradually optimized to achieve the best effect. The following is based on 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 String StartDate = NULL; Static Private String Totalweek = NULL; Synchronize TDATE == NULL || Totalweek == NULL) {FileInputStream is = NULL; Properties dbprops = New Properties (); Try {is = New FileinputStream (FilePath); dbprops.load (is); startDate = dbprops.getProperty ("StartDate"); Totalweek = ("Totalweek");} Catch (Exception E) {System .err.println ("Can't read attribute files." + "Please make sure that db.properties are in the path specified by classpath");}}} public static string getstartDate () {if (tartDate == null); Return StartDate;} Public Static String GettTotalweek () {if (StartDate == NULL) Loads (); Return Totalweek;}}
Although the above method can also get configuration file content, the biggest problem is the positioning of the file path (that is, the FilePath value in the code). When the absolute positioning is adopted, if the project is moved to another disk formation, it is running. You need to modify the source code, otherwise an error will be reported, but if the relative path is used, when the Stweek class is moved to another bag, you still need to modify the source code, otherwise the error will be reported. Method 2 can solve this problem, but there are still some problems
Method two:
public class stweek {inputStream is = null; proprties dbprops = null; public stweek () {// todo auto-sentence constructor stub is = GetClass (). tresourceasstream ("/date.properties"); dbprops = New Properties (); Try {dbprops.load (is);} Catch (Exception E) {system.errr.println ("Can't read attribute files." + "Please make sure that db.properties are in the path specified by classpath");} public string getStartDate () {string sd = null; sd = dbprops.getproperty ("startDate"); Return SD;} Public String GettTotalweek () {string toTalweek = NULL; Totalweek = dbprops.getProperty ("Totalweek"); Return Totalweek;} }
The advantage of this method is that there is no need to point out the absolute path of the configuration file, and whether it is placed in another package or the entire project is moved to the other disk formation, the code can still run normally. On the problem of the file, there is still a major defect in this method, because we often want to configure the file to cache in memory, so that you need to access the hard disk when you read it every time you read (visit the existence of existence too much time). To this end I hope to use static variables and static methods to cache and obtain variables, and at the same time can achieve dynamic loading (load) of these values, then the problem comes, because getclass (). GetResourceASStream ("/Date.properties"); In the constructive function (the same shoes can be tested by yourself), you can't use this method in the dynamic load, what should I do, and see the third method
Method three:
Import java.io.inputStream; Import Java.util.properties; Public Class Stweek {Static Private String StartDate = NULL; Static Private String Totalwek = null; static {loads ();} Synchronized Static Public Void Loads () {if (StartDate == NULL || Totalweek == NULL) {InputStream is = Stweek.class.getResourceasstream ("/Date.properties"); try {dbprops.load (is); startDate = dbprops.getproperty ("STARTDATE"); Totalweek = dbprops.GetProperty ("Totalweek");} Catch (Exception E) {System.err.println ("Can't read attribute files." + "" " The path specified by the asspath Middle ");}}} Public Static String GetStartDate () {if (StartDate == NULL) Loads (); Return StartDate;} Public Static GetTotalweek () {if () {if ATE == NULL) Loads (); Return Totalweek; }}
This method can not only cache the content of the file, but also automatically loads the content of the configuration file to memory. The user does not need to consider the process of manual loading. You only need to directly call the stweek.getstAtedate () where you need it. Because it is a static method, it is not necessary to create the right object in advance), so if there is a cache in the memory, the function will directly read the data in the memory and save time. If there is no cache, you don’t need to worry about it. Users don't need to know how it is realized at all. I just need to know that I can directly call the function to get the desired value, haha, simple note: (have nothing to do with the above, your test)
It is hoped that this article is helpful to everyone's Java program design.