Java Read Properties configuration file

Source: Internet
Author: User

Java read. Properties configuration file

These two days to do the Java project, use the property file, to the online search information, long time also did not find a comfortable way to let me read into the. properties file in the property value. Very depressed, on-line to get the attribute value is probably the following method, the following three methods are gradually optimized to achieve the best results below are date.properties files for example. The file is placed under the SRC folder. The contents of the file are

startdate=2011-02-07
Totalweek=25

Method One:

 

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 ("Cannot read property file." +                     "Make sure db.properties is in the path specified by 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 get the contents of the configuration file, the biggest problem is the location of the file path (that is, the FilePath value problem in the code). When using absolute positioning, assume that project is moved to another drive letter. You need to change the source code, or you will get an error. But assuming that the relative path, when the Stweek class moved to another package, or to change the source code, otherwise it will be an error. So this method is too limited, not good. The following method can solve the problem, but it still has some problems

Method Two:

public class stweek{  inputstream is = null; Properties dbprops = Null;public Stweek () {//TODO auto-generated Constructor stubis = GetClass (). getResourceAsStream ("/ Date.properties ");d Bprops = new properties ();  try {  dbprops.load (is);}  catch (Exception e) {   System.err.println ("Cannot read the properties file." + "Make sure the db.properties is in the path specified in the 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 approach is that you do not need to indicate the absolute path to the configuration file, and either put the Stweek class in another package. or move the entire project to another drive letter, the code will still work, and there will be no problem finding the file. But there is still a major flaw in this approach. Because we often want the configuration file to be in memory. This makes it unnecessary to access the hard drive every time it is read (visiting external memory is a waste of time), so we want to use static variables, static methods to cache and get variables, and at the same time can implement dynamic loading of these values (load), then the problem is due to getclass (). getResourceAsStream ("/date.properties"); This sentence can only be in today's constructor (the same shoes can test their own), dynamic load does not use this method, how to 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 Totalweek = Null;static{loads ();} Synchronized static public void loads () {if (StartDate = = NULL | | totalweek = = NULL) {InputStream is = Stweek.class.getResour Ceasstream ("/date.properties"); Properties Dbprops = new properties (); try {dbprops.load (is); startdate = Dbprops.getproperty ("StartDate"); Totalweek = Dbprops.getproperty ("Totalweek");} catch (Exception e) {System.err.println ("cannot read the properties file." + "Make sure the db.properties is in the path specified in the Classpath");}}} public static String Getstartdate () {if (startdate==null) loads (); return startdate;} public static String Gettotalweek () {if (startdate==null) loads (); return totalweek;}}


This method not only can cache the configuration file contents, but also can do the contents of the self-loading configuration file to memory, the user does not have to consider the manual loading process, only need to use the place directly call Stweek.getstartdate () can be (because it is a static method. You don't have to create a pair of images in advance, so assuming that there's a cache in memory, the function will read the data in memory directly, saving time. Assuming there is no cache, don't worry, the system will voluntarily load for you. The user does not have to know how it is implemented. Just need to know that I can call the function directly to get the desired value. Oh. It's simple.

Remark: (not related to above, own test)


Java Read Properties configuration file

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.