MAVEN organization Web project reads Web-inf properties file

Source: Internet
Author: User

Development time to read the configuration information of the properties file, but the properties file location and properties access to different reading methods are different

1. General access: Java projects and Web projects.

2. File location: Same directory as source file and different from source directory

    • The Java project reads the properties file method in the same directory as the source file, reading from the main function

Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.util.properties;public class Propertiesutils {public static void main (string[] args) { Properties Properties = new properties (); try {inputstream in = PropertiesUtils.class.getResourceAsStream ("./ Db.properties ");//or directly written as" Db.properties "properties.load (in); String url = properties.getproperty ("url"); String userName = Properties.getproperty ("user"); String Password = properties.getproperty ("password"); System.out.println ("url=" +url+ ", username=" +username+ ", password=" +password);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}

The parent or child directory is just the appropriate method path. The key to reading the properties file is the ability to access the properties file, which is read the same way, mainly because of different methods of access.


    • Read the properties file method in the MAVEN organization's Web project

The following methods are accessed in the case of an HTTP request


Public map<string, String> Getdbinfo () {map<string, string> dbInfo = new hashmap<string, string> (); try {String URL = this.getclass (). GetResource (""). GetPath ();              String path = url.substring (0, Url.indexof ("Web-inf")) + "web-inf/db.properties";              Properties Config = new properties ();              InputStream in = new FileInputStream (path);            Config.load (in);             String Dburl = Config.getproperty ("<span style=" font-family:arial, Helvetica, Sans-serif; " >dbUrl</span> ");            Dbinfo.put ("url", Dburl);            Dbinfo.put ("UserName", Config.getproperty ("user"));            Dbinfo.put ("Password", Config.getproperty ("password")); catch (Exception e) {e.printstacktrace ();} return dbInfo;}


The above two differences mainly lie in getResourceAsStream and GetResource.


Appendix:

Usage of getResourceAsStream in Java:
First, there are several getresourceasstream in Java:
1. Class.getresourceasstream (String Path): path does not start with '/' by default, the resource is fetched from the package where the class is located, and the '/' begins with the classpath root. It only constructs an absolute path through path, and eventually it gets the resources from the ClassLoader.

2. Class.getClassLoader.getResourceAsStream (String path): The default is obtained from the classpath root, path cannot start with '/', and eventually the resource is obtained by ClassLoader.

3. ServletContext. getResourceAsStream (String path): The default is to fetch resources from the WebApp root directory, and whether or not Tomcat starts with '/' does not matter, of course, this is related to the specific container implementation.

getResourceAsStream usage is roughly the following:
First: The file to be loaded and the. class file are in the same directory, for example: Com.demo has a class test.class, and a resource file Myfile.xml
Then, you should have the following code:
Test.class.getResourceAsStream ("Myfile.xml");

Second: In the subdirectory of the Me.class directory, for example: Com.demo under the class Test.class, while the Com.demo.file directory has a resource file Myfile.xml
Then, you should have the following code:
Test.class.getResourceAsStream ("File/myfile.xml");
or the following:
Test.class.getResourceAsStream ("./file/myfile.xml");
Third: Not under the Test.class directory, also not under the subdirectory, for example: Com.demo under the class Test.class, while in the Com.demo.file directory has a resource file Myfile.xml
Then, you should have the following code:
Test.class.getResourceAsStream ("/com/demo/file/myfile.xml");


To summarize, maybe it's just two ways of writing
First: There is "/" in front
"/" represents the root of the project, such as the project name MyProject, "/" represents the MyProject
Test.class.getResourceAsStream ("/com/demo/file/myfile.xml");

Second: There is no "/" in front of the directory that represents the current class (the current path can also be represented by "./")
Test.class.getResourceAsStream ("Myfile.xml");
Test.class.getResourceAsStream ("File/myfile.xml");
or the following:
Test.class.getResourceAsStream ("./myfile.xml");
Test.class.getResourceAsStream ("./file/myfile.xml");



MAVEN organization Web project reads Web-inf properties 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.