Java Read the properties file

Source: Internet
Author: User

Method One, read by java.util.Properties

Properties p=New  properties (); // p requires the InputStream object to read the file, and there are several ways to get InputStream: // 1, through the absolute path: InputStream is=new FileInputStream (filePath); // 2, through the Class.getresourceasstream (path); //  p.load (InputStream is), Is.close ();p. getString (String (key)

Method Two, read by Java.util.ResourceBundle

ResourceBundle rb=resourcebundle.getbundle (PackageName); rb.getstring (String key);

The difference between Class.getresourceasstream and Classloader.getresourceasstream
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.

4. The application built-in object under JSP is an implementation of the above ServletContext.

Java Read the 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.