Java read the Properties configuration file Method _java

Source: Internet
Author: User

The example in this article describes how Java reads the properties configuration file. Share to everyone for your reference. The specific analysis is as follows:

These two days to do Java projects, use the attribute file, to the Internet to look up information, I didn't find a satisfactory way to read it. properties file in the attribute value, is very depressed, on the Internet to get property values are probably the following methods, the following three methods are gradually optimized to achieve the best results the following are date.properties files as an example, the file is placed in the SRC directory, the contents of the file is:

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 the property file." + "Make sure Db.properties is in classpath specified path");
   }} 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, but the biggest problem is the location of the file path (that is, the code in the FilePath value problem), when the absolute positioning, if the project moved to another letter under the operation, you need to modify the source code, otherwise it will be an error, but if the use of relative path, When the Stweek class moved to another package, or to modify the source code, or it will error, so that this method is too limited, not good, the following methods can solve the problem, but it is still some problems

Method Two:

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 ("Cannot read the property file." +
   "Make sure Db.properties is in classpath specified path");
  }
 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 of the configuration file. and whether it's putting the Stweek class in another package, or moving the entire project to another disk, the code still works and there's no problem finding the file, but there's still a major flaw in this approach, Because we tend to want the configuration file to be cached in memory, this does not need every read to access the hard disk (access to the memory is too waste time), so we want to use static variables, static methods to cache and get variables, and can implement the dynamic loading of these values (load), then the problem comes, Because GetClass (). getResourceAsStream ("/date.properties"); This sentence can only appear in the constructor (the same shoe can test itself), dynamic load can 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 P
 rivate String startdate = null;
 static private String Totalweek = null;
 static{loads (); Synchronized static public void loads () {if (StartDate = null | | | totalweek = = NULL) {InputStream is = stweek.cl
   Ass.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 ("cannot read the property file." + "Make sure Db.properties is in classpath specified path");
  }} 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 to automatically load the contents of the configuration file to memory, the user does not have to consider the process of manual loading, only need to use the place directly call Stweek.getstartdate () can (because it is a static method, You do not have to create the image in advance, so if there is a cache in memory, function will directly read the data in memory, save time, if there is no cache do not worry, the system will automatically load for you, users do not know how it is achieved, just know that I can directly call the function to get the desired value on the line, hehe, It's easy.
Remark: (independent of the above, own test)

I hope this article will help you with your Java programming.

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.