Tools class for reading properties configuration files

Source: Internet
Author: User
Tags server port

General small Engineering, properties configuration files are created in the SRC root directory

Like Data.properties, the code is as follows

<!--proxy server address-->
proxy=192.168.0.1
<!--proxy server port number-->
port=8080
<!--login proxy username-->
username=username
<!--login Agent password-->
password=password
<!--picture storage path-->
imgpath=c:/ xml/
<!--XML filename-->
xmlname=uploadrequest.xml

<!--1. Build Fat jar contains dom4j-->
<!--2 cmd-generated jar path Java-jar Xx.jar-->
<!--properties configuration file update Difficult-->

The tool class used to read data is written as follows:

Package com.main.util;

Import java.io.IOException;
Import java.util.Properties;

/** * * * * * * *
 @author mo _ly 2012-3-30/public
class tools
	static Properties P = new properties ();

	/**
	 * Read Properties profile information/
	static{
		try {
			p.load (Tools.class.getClassLoader). getResourceAsStream ("data.properties"));
		} catch (IOException e) {
			e.printstacktrace (); 
		}
	}
	/**
	 * The value obtained by key * * Public
	static string GetValue (String key)
	{return
		p.getproperty ( key);
	}


So, when calling a constant in a configuration file, just call the GetValue () method, such as

Tools.getvalue ("PORT")

That is 8080


========================================================================


In addition, some of the functionality in the project is often isolated as Java project, and then made into a jar package for other projects to invoke. If the configuration file information needs to be read in the jar package, it is rarely put into the jar package because it is inconvenient to modify, and more often than not, a jar packet is used to read the external configuration file.

Properties configuration files removed from the project, first placed under the project, and the SRC parallel path. As shown in figure


To read the configuration File tool class tools make the following changes:

Package com.main.util;

Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;

/** * * * * * * 
 @author mo-_ly 2012-3-30/public
class Tools {

    private static Properties P = new properties ();
    The static {
        try {
            //System.getproperty ("User.dir") obtains the absolute path to the project, and then the path to the assembly configuration file
            reads the system external configuration file (that is, the jar Package foreign language)--- External engineering reference to the jar package requires that the Config directory be created under the engineering configuration file
            String FilePath = System.getproperty ("User.dir") + "/config/data.properties ";
            InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
            P.load (in);
        } catch (IOException e) {
            System.out.println ("Error reading configuration information.") ");
        }
    }

    /**
     * Gets value from key * *
     public
    static string GetValue (String key) {return
        p.getproperty (key);
    }
}

Remove config First when you make a jar packageWith the fat jar packaged, put the generated jar Bundle and Config folder on the same path.

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.