In daily use, the properties file is often read, and some java Methods collected on the Internet to read properties are summarized as follows:
1. Use the load () method of Java. util. properties class
For example:
Inputstream is = new bufferedinputstream (New fileinputstream (new file (path of the properties file); properties Properties = new properties (); properties. Load (is );
2. Use the getbundle () method of the Java. util. resourcebundle class
For example:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3. Use the constructor of the Java. util. propertyresourcebundle class
For example:
InputStream is = new BufferedInputStream(new FileInputStream(name));ResourceBundle rb = new PropertyResourceBundle(is);
4. Use the getresourceasstream () method of the class variable
For example:
Inputstream in = Class Name. Class. getresourceasstream (name); properties P = new properties (); p. Load (in );
5. Use the getresourceasstream () method of Java. Lang. classloader obtained by class. getclassloader ()
For example:
Inputstream in = Class Name. Class. getclassloader (). getresourceasstream (name); properties P = new properties (); p. Load (in );
6. Use the getsystemresourceasstream () Static Method of the Java. Lang. classloader class
For example:
InputStream in = ClassLoader.getSystemResourceAsStream(name);Properties p = new Properties();p.load(in);
Some code is attached below
Resourcebundleadapter class:
Package COM. dengsilinming. read. properties; import Java. util. enumeration; import Java. util. locale; import Java. util. properties; import Java. util. propertyresourcebundle; import Java. util. resourcebundle;/*** todo comment of resourcebundleadapter ** @ author dengsilinming * @ version $ ID: resourcebundleadapter. java 1:35:47 $ */public class resourcebundleadapter extends properties {/*****/priv Ate static final long serialversionuid = 1l; Public resourcebundleadapter (resourcebundle Rb) {assert (rb instanceof Java. util. propertyresourcebundle); this. RB = RB; enumeration <string> E = RB. getkeys (); While (E. hasmoreelements () {object o = E. nextelement (); this. put (O, Rb. getObject (string) O) ;}} private resourcebundle RB = NULL; Public resourcebundle getbundle (string basename) {return res Ourcebundle. getbundle (basename);} public resourcebundle getbundle (string basename, locale) {return resourcebundle. getbundle (basename, locale);} public resourcebundle getbundle (string basename, locale, classloader loader) {return resourcebundle. getbundle (basename, locale, loader);} public enumeration <?> Getkeys () {return RB. getkeys ();} public locale getlocale () {return RB. getlocale ();} public object GetObject (string key) {return RB. getObject (key);} Public String getstring (string key) {return RB. getstring (key);} Public String [] getstringarray (string key) {return RB. getstringarray (key);} protected object handlegetobject (string key) {return (propertyresourcebundle) Rb ). handlegetobject (key );}}
Operateproperties class:
Package COM. dengsilinming. read. properties; import Java. io. bufferedinputstream; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. util. enumeration; import Java. util. locale; import Java. util. properties; import Java. util. propertyresourcebundle; import Java. util. resourcebundle;/*** todo comment of jproperties ** @ author dengsilinming * @ version $ ID: jpropertie S. java 2013-1-30 1:34:32 $ */public class operateproperties {public final static int by_properties = 1; public final static int by_resourcebundle = 2; public final static int by_propertyresourcebundle = 3; public final static int by_class = 4; public final static int by_classloader = 5; public final static int by_system_classloader = 6; Public Final Static Properties loadproperties (final string Name, final int type) throws ioexception {properties P = new properties (); inputstream in = NULL; If (type = by_properties) {In = new bufferedinputstream (New fileinputstream (name); Assert (in! = NULL); p. Load (in);} else if (type = by_resourcebundle) {resourcebundle RB = resourcebundle. getbundle (name, locale. English); Assert (rb! = NULL); P = new resourcebundleadapter (RB);} else if (type = by_propertyresourcebundle) {In = new bufferedinputstream (New fileinputstream (name); Assert (in! = NULL); resourcebundle RB = new propertyresourcebundle (in); P = new resourcebundleadapter (RB);} else if (type = by_class) {assert (operateproperties. class. equals (New operateproperties (). getclass (); In = operateproperties. class. getresourceasstream (name); Assert (in! = NULL); p. load (in); // return New jproperties (). getclass (). getresourceasstream (name);} else if (type = by_classloader) {assert (operateproperties. class. getclassloader (). equals (New operateproperties (). getclass (). getclassloader (); In = operateproperties. class. getclassloader (). getresourceasstream (name); Assert (in! = NULL); p. load (in); // return New jproperties (). getclass (). getclassloader (). getresourceasstream (name);} else if (type = by_system_classloader) {In = classloader. getsystemresourceasstream (name); Assert (in! = NULL); p. Load (in);} If (in! = NULL) {In. close ();} return P;} public static class resourcebundleadapter extends properties {/*****/Private Static final long serialversionuid = 1l; Public resourcebundleadapter (resourcebundle RB) {assert (rb instanceof Java. util. propertyresourcebundle); this. RB = RB; enumeration <string> E = RB. getkeys (); While (E. hasmoreelements () {object o = E. nextelement (); this. put (O, Rb. getObject (S Tring) O) ;}} private resourcebundle RB = NULL; Public resourcebundle getbundle (string basename) {return resourcebundle. getbundle (basename);} public resourcebundle getbundle (string basename, locale) {return resourcebundle. getbundle (basename, locale);} public resourcebundle getbundle (string basename, locale, classloader loader) {return resourcebundle. getbundle (basename, locale, Loader);} public enumeration <?> Getkeys () {return RB. getkeys ();} public locale getlocale () {return RB. getlocale ();} public object GetObject (string key) {return RB. getObject (key);} Public String getstring (string key) {return RB. getstring (key);} Public String [] getstringarray (string key) {return RB. getstringarray (key);} protected object handlegetobject (string key) {return (propertyresourcebundle) Rb ). handlegetobject (key );}}}
Propertiestest class:
Package COM. dengsilinming. read. properties; import Java. util. properties; import JUnit. framework. testcase;/*** todo comment of propertiestest *** @ author dengsilinming * @ version $ ID: propertiestest. java 1:40:52 $ */public class propertiestest extends testcase {operateproperties jproperties; string key = "helloworld. title "; string value =" Hello world! "; Public void testloadproperties () throws exception {string name = NULL; properties P = new properties (); name =" F: /workspace/dailypractise/src/COM/dengsilinming/read/properties/localeproperties. properties "; P = operateproperties. loadproperties (name, operateproperties. by_properties); assertequals (value, P. getproperty (key); name = "com. dengsilinming. read. properties "; P = operateproperties. loadproperties (name, operateproperties. by_resourcebundle); assertequals (value, P. getproperty (key); assertequals (value, (operateproperties. resourcebundleadapter) P ). getstring (key); name = "F:/workspace/dailypractise/src/COM/dengsilinming/read/properties/localeproperties. properties "; P = operateproperties. loadproperties (name, operateproperties. by_propertyresourcebundle); assertequals (value, P. getproperty (key); assertequals (value, (operateproperties. resourcebundleadapter) P ). getstring (key); name = "\ com \ dengsilinming \ read \ properties \ localeproperties. properties "; P = operateproperties. loadproperties (name, operateproperties. by_system_classloader); assertequals (value, P. getproperty (key); name = "\ com \ dengsilinming \ read \ properties \ localeproperties. properties "; P = operateproperties. loadproperties (name, operateproperties. by_classloader); assertequals (value, P. getproperty (key); name = "\ properties \ localeproperties. properties "; P = operateproperties. loadproperties (name, operateproperties. by_class); assertequals (value, P. getproperty (key ));}}
The code attached above will report errors during runtime. You can modify it by yourself, which is a very easy issue. Do not be lazy.
One thing to note is: this is an article I saw on the Internet and I slightly changed some code. I am sorry for the original author because I didn't pay too much attention to the source of the article.