Get the content of the properties file on tomcat-Facilitate modification, decoupling, and tomcatproperties of the file storage location

Source: Internet
Author: User

Get the content of the properties file on tomcat-Facilitate modification, decoupling, and tomcatproperties of the file storage location

During java web development, reading or storing files is often used. Where is the default path of this file? Writing to the program is obviously acceptable, but this is not conducive to the location. If one day the project is transplanted from window to linux, or the path to save the file changes, you need to find it in the source code, to replace the file. This is not only inefficient, but the Coupling Degree of the program will be too high. Here I use a properties file to store the file storage path, you need to save or read the path stored by properties.

1. Path of my stored propeities File

Because the partitioning on linux and Windows is different, I put the properties file of the file path in the project, so we can get the file through the tomcat path.

 

2. properties File Content

The file path I used here is/, which is compatible with linux and Windows.We recommend that you use File. separator as the separator to ensure compatibility with different operating systems.

filePath=E:/file

 

 

3. Obtain the path of the file

String dir = System. getProperty ("user. dir "); // obtain the working path of tomcat System. out. println (dir); // obtain the filedir that stores the file storage location. properties file path String dir2 = dir. substring (0, dir. length ()-4) + File. separator + "webapps" + File. separator + "NGBOSSmonitor" + File. separator + "WEB-INF" + File. separator + "classes" + File. separator + "META-INF" + File. separator + "config" + File. separator + "filedir. properties ";

 

Dir obtains: D: \ Tocat \ tomcat6.0.37.\ Bin

The dir2 I obtained is D: \ Tocat \ tomcat6.0.37 \ webapps \Your project \ WEB-INF \ classes \ META-INF \ config \Filedir. properties

 

4. Use the properties FileTo obtain the value of filePath, that is, to obtain E:/file

/*** Obtain the value of the key in the filePath [properities file, * @ param filePath properities file path [including properities file] * @ param key the key value to be searched * @ return key the value */public String GetValueByKey (String filePath, String key) {Properties pps = new Properties (); try {InputStream in = new BufferedInputStream (new FileInputStream (filePath); pps. load (in); String value = pps. getProperty (key); // System. out. println (key + "=" + value); in. close (); return value;} catch (IOException e) {e. printStackTrace (); return null ;}}

 

Here, you have obtained the filePath value in filedir. properties.

5. Summary

In the development process, it is very important to make the program uncoupled. The lower the coupling degree, the easier it is to develop and modify.

 

  Thank you: Thank you for your patience!

 

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.