Java program reads the properties configuration file under Tomcat

Source: Internet
Author: User

The code is as follows:

Locate the Tomcat/etc/wx folder private static String Getpropfolderpath () {/* Properties p =system.getproperties();P.list (System.out); * *Get the path to tomcat String path =System.getproperty("Catalina.home");       if (stringutils.isempty (path)) {path = QueryQRCodeUrlController.class.getResource ("/"). GetPath ();     Path = new File (path). GetParent () + file.separator; } else {path = path +File.separator+ Tomcat_path;   } return path; }Initializing configuration information ************************static{Properties P=new properties ();InputStream In=null;try {In=new FileInputStream (Getpropfolderpath () +wx_properties);P.load (in);VALUE = P.getproperty ("Wx.serviceid");PWD = P.getproperty ("Wx.pwd");} catch (Exception e) {Logger.error (E.tostring ());}finally{if (in!=null) {try {In.close ();} catch (IOException e) {Logger.error (E.tostring ());}}}}

1:file.separator the system-related default name delimiter.

The path separators under windows are not the same as the path separators under Linux, and when you use absolute paths directly, cross-platform bursts out "No such file or diretory" exceptions.

For example, to create a test.txt file under the temp directory, which should be written under Windows:
File File1 = new file ("C:\tmp\test.txt");
Under Linux, this is the case:
File File2 = new file ("/tmp/test.txt");

If you want to consider cross-platform, it's best to write this:
File MyFile = new file ("C:" + file.separator + "tmp" + file.separator, "test.txt");

The file class has several static fields similar to separator, which are system-related and should be used as much as possible when programming.

2:system.getproperties (): This method is to get many property names and value values for the current system. After these key-value pairs are obtained, the value values can be obtained in the following ways:

System.getproperty ("Catalina.home"); If you know these properties of the system, you can obtain the corresponding value value directly from this method.

3: Static code block:

static{

System.out.println ();

}

Used to initialize some information, and after the service is started, the class is executed when it is loaded. Executes only once.

Java program reads the properties configuration file under Tomcat

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.