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 );
}
}