How to get the current path and properties file in Java

Source: Internet
Author: User

Method for obtaining the current path in Java

1. system. getproperty ("user. dir") Relative Path of the current user directory.

2. This. getclass (). getresource ("") is also the absolute URI path of the current classpath.

3. classloader. getsystemresource ("") is also the absolute URI path of the current classpath.

4. thread. currentthread (). getcontextclassloader (). getresource ("") is also the absolute URI path of the current classpath.

 

How to read the properties file in Java

1. Use the load () method of Java. util. properties class
Example: inputstream in = lnew bufferedinputstream (New fileinputstream (name ));
Properties P = new properties ();
P. Load (in );

2. Use the getbundle () method of the Java. util. resourcebundle class
Example: resourcebundle RB = resourcebundle. getbundle (name, locale. getdefault ());

3. Use the constructor of the Java. util. propertyresourcebundle class
Example: inputstream in = new bufferedinputstream (New fileinputstream (name ));
Resourcebundle RB = new propertyresourcebundle (in );

4. Use the getresourceasstream () method of the class variable
Example: inputstream in = jproperties. Class. getresourceasstream (name );
Properties P = new properties ();
P. Load (in );

5. Use the getresourceasstream () method of Java. Lang. classloader obtained by class. getclassloader.

Example:

Inputstream in = jproperties. Class. getclassloader (). getresourceasstream (name );
Properties P = new properties ();
P. Load (in );

6. Use the getsystemresourceasstream () Static Method of the Java. Lang. classloader class
Example: inputstream in = classloader. getsystemresourceasstream (name );
Properties P = new properties ();
P. Load (in );

7. You can use the getresourceasstream () method of javax. servlet. servletcontext in servlet.
Example: inputstream in = context. getresourceasstream (PATH );
Properties P = new properties ();

P. Load (in );

 

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.