How to obtain the project-defined property file in Web Development

Source: Internet
Author: User

In web development, how does one obtain the property file defined by the project.

I wrote a few articles about some basic applications in the Web development process.
Hope to help beginners

In other words, I hope that the experts can give corrections.

Property file name: properyname. properties; Location: Under the classes directory of the project.

Content: AAA = luyang

(The class path is used for obtaining)

External call method:

Propmngr Mgr = propmngr. getinstance ();
String content = getproperty ("AAA ");

// AAA is the key defined in property. The result is: luyang

======================================

Package org. luyang;

Import java. util. enumeration;
Import java. util. iterator;
Import java. util. Map;
Import java. util. missingresourceexception;
Import java. util. properties;
Import java. util. resourcebundle;

/**
* Property Management
*
* @ Version 1.0
* @ Author luyang
*/
Public class propmngr {

Private Properties props = new properties ();

Public static propmngr instances = NULL;

Public static string name = "properyname ";

Public propmngr getinstance (){
If (null = instances ){
Instances = new propmngr ();

}
Return instances;
}

/**
* Private constructor
*/
Private propmngr (){
Init (name );
}

/**
* Initial property
* @ Param spropfilepathname class path of the property
* @ Throws missingresourceexception
*/
Public synchronized void Init (string spropfilepathname)
Throws missingresourceexception {

String propfile = spropfilepathname;
Resourcebundle bundle = resourcebundle. getbundle (propfile );
Enumeration enume = bundle. getkeys ();
Object key = NULL;
Object value = NULL;
While (enume. hasmoreelements ()){
Key = enume. nextelement ();
Value = bundle. getstring (key. tostring ());
Props. Put (Key, value );
}

// Use DEBUG
Iterator it = props. entryset (). iterator ();
While (it. hasnext ()){
Map. Entry xx = (Map. Entry) it. Next ();
System. Out. println (XX. getkey ());
System. Out. println (XX. getvalue ());
}
}

/**
* Get property
*
* @ Param key prpperty's key
* @ Return string prpperty content

*/
Public String getproperty (string key ){
Return props. getproperty (key );
}
}
 

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.