Java Read properties

Source: Internet
Author: User

1  PackageCom.avonaco.web.excel; 2   3 ImportJava.io.BufferedInputStream; 4 ImportJava.io.FileInputStream; 5 ImportJava.io.InputStream; 6 Importjava.util.Enumeration; 7 ImportJava.util.HashMap; 8 ImportJava.util.Map; 9 Importjava.util.Properties; Ten ImportOrg.apache.commons.logging.Log;  One Importorg.apache.commons.logging.LogFactory;  A    -  Public classPropertiesutil { -        the     Private Static FinalLog log = Logfactory.getlog (propertiesutil.class);  -    -     Private StaticProperties Properties =NewProperties ();  -        +      Public Static FinalString US = "Applicationresources.properties";//English -    +      Public Static FinalString China = "applicationresources_zh_cn.properties";//English A        at      Public Static FinalString china_user = "Applicationresourcesusermanager_zh_cn.properties" ;  -        -      Public Static FinalString us_user = "Applicationresourcesusermanager.properties";  -     /**  - * @desc get all the key and value -      * @paramPropertiesname in      * @return  -      * @author: Lic to * @datetime: 2011-12-14 a.m. 9:50:21 +      */   -@SuppressWarnings ({"Rawtypes", "unchecked" })   the      Public StaticMap getProperties (String propertiesname) { *            $Map map =NewHashMap (); Panax Notoginseng            -         if(Propertiesname! =NULL&&!propertiesname.equals ("") ){   theString URL =Propertiesutil.getpropertiespath (propertiesname);  +             if(log.isdebugenabled ()) { ALog.debug (">>>>>>>>>>>>>>>>>>> Get Properties File path--" +URL);  the             }   +                -             Try {   $InputStream in =NewBufferedinputstream (Newfileinputstream (URL));  $ PropertiesUtil.properties.load (in);  -                    -System.out.println ("1");  theEnumeration en =PropertiesUtil.properties.propertyNames ();  -                     while(En.hasmoreelements ()) {WuyiString key =(String) en.nextelement ();  theString value =PropertiesUtil.properties.getProperty (key);  - map.put (key, value);  Wu System.out.println ();  -                     if(log.isdebugenabled ()) { AboutLog.debug (">>>>>>>>>>>>>>>>>>>>>>>> >> "+ Propertiesname +"--: "+ key +" >>> value = "+value);  $                     }   -                    }   -}Catch(Exception e) { -Log.debug (">>>>>>>> Propertiesutil get all key and value error:" +e);  A E.printstacktrace ();  +             }   the         }   -            $            the         returnmap;  the     }   the        the        -        in     /**  the * @desc The value of properties based on key the      * @paramPropertieskey About      * @returnMap the      * @author: Lic the * @datetime: 2011-12-14 a.m. 10:02:13 the      */   +@SuppressWarnings ("Rawtypes")   -      Public Staticstring Getpropertiesvalue (String propertieskey, string filename) { the               BayiString value = "" ;  the                the             if(Propertieskey! =NULL&&!propertieskey.equals ("") ){   -String Pkey =filename;  -String URL =propertiesutil.getpropertiespath (filename);  the                 Try {   theInputStream in =NewBufferedinputstream (Newfileinputstream (URL));  the //this.properties.load (in);  the PropertiesUtil.properties.load (in);  -System.out.println ("1");  the //enumeration en = Properties.propertynames ();  theEnumeration en =PropertiesUtil.properties.propertyNames ();  the                         while(En.hasmoreelements ()) {94String key =(String) en.nextelement ();  the                            if(Key.equals (Propertieskey)) { the //value = This.properties.getProperty (key);  theValue =PropertiesUtil.properties.getProperty (key); 98                            }   About                        }   -}Catch(Exception e) {101 E.printstacktrace (); 102                 }  103             }  104                the               106             returnvalue; 107         }  108           109        the   111     /**  the * @desc Get the path based on the properties file name113      * @paramPropertieskey the      * @returnMap the      * @author: Lic the * @datetime: 2011-12-13 pm 15:53:58117      */  118      Public Staticstring Getpropertiespath (String propertiesname) {119                -String Path = ""; 121             if(Propertiesname! =NULL&&!propertiesname.equals ("") ){  122Path = Propertiesutil.class. getClassLoader (). GetResource (Propertiesname). toString (). SUBSTRING (6); 123             }  124                the             returnpath; 126     }  127            -           129}

Java Read properties

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.