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