Properties class in Java and Read property values in attributes

Source: Internet
Author: User

In the application of the project, often put some configuration into the properties file, in the code application to read the properties file, you need a special class properties class, through this class can be read.

For further understanding and learning, see: In-depth understanding and learning properties Reference

This shows the Help class in the project that reads the properties configuration file, and the code can be used directly:

Importjava.io.IOException;Importjava.util.Properties; Public classpropertiesutil{ Public Static FinalString File_path = "Properties/upload.properties"; Get the corresponding value by passing in the path and key Public Staticstring GetValue (string path, String key) {Properties Properties=NewProperties (); Try{properties.load (propertiesutil).class. getClassLoader (). getResourceAsStream (path)); }        Catch(IOException e) {Throw NewRuntimeException ("File Read Failed ...", E); }        returnProperties.getproperty (key); }//Get the corresponding value directly via key Public Staticstring GetValue (String key) {Properties Properties=NewProperties (); Try{properties.load (propertiesutil).class. getClassLoader (). getResourceAsStream (File_path)); }        Catch(IOException e) {Throw NewRuntimeException ("File Read Failed ...", E); }        returnProperties.getproperty (key); }}

Properties class in Java and Read property values in attributes

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.