Properties file read/write tool class Propertiesutil.java

Source: Internet
Author: User

Tag: value gets read static catch filename util trace information

ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.util.Properties;/** *  * @author   * */ Public classPropertiesutil {PrivateString properiesname = "";  PublicPropertiesutil () {} PublicPropertiesutil (String fileName) { This. Properiesname =FileName; }        /*** Get values by key *@paramKey *@return     */     Publicstring ReadProperty (String key) {String value= ""; InputStream is=NULL; Try{ is= Propertiesutil.class. getClassLoader (). getResourceAsStream (Properiesname); Properties P=NewProperties ();            P.load (IS); Value=P.getproperty (key); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{is.close (); } Catch(IOException e) {e.printstacktrace (); }        }        returnvalue; }    /*** Get the entire configuration information *@return     */     PublicProperties GetProperties () {Properties P=NewProperties (); InputStream is=NULL; Try{ is= Propertiesutil.class. getClassLoader (). getResourceAsStream (Properiesname);        P.load (IS); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{is.close (); } Catch(IOException e) {e.printstacktrace (); }        }        returnp; }    /*** Key-value write config file *@paramKey *@paramvalue*/     Public voidWriteProperty (string key, String value) {InputStream is=NULL; OutputStream OS=NULL; Properties P=NewProperties (); Try{ is=NewFileInputStream (properiesname);//is = PropertiesUtil.class.getClassLoader (). getResourceAsStream (properiesname);P.load (IS);//OS = new FileOutputStream (PropertiesUtil.class.getClassLoader (). GetResource (Properiesname). GetFile ()); OS =NewFileOutputStream (properiesname);            P.setproperty (key, value);            P.store (OS, key);            Os.flush ();        Os.close (); } Catch(Exception e) {e.printstacktrace (); } finally {            Try {                if(NULL!=is ) is.close (); if(NULL!=OS) os.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }     Public Static voidMain (string[] args) {//sysconfig.properties (config file)Propertiesutil p =NewPropertiesutil ("Sysconfig.properties"); System.out.println (P.getproperties (). Get ("Db.url")); System.out.println (P.readproperty ("Db.url")); Propertiesutil Q=NewPropertiesutil ("Resources/sysconfig.properties"); Q.writeproperty ("Myutils", "Wang"); System.exit (0); }}

Properties file read/write tool class Propertiesutil.java

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.