Java Web Engineering Read and modify configuration files

Source: Internet
Author: User

This blog is more detailed than its own explanation:

Http://blog.sina.com.cn/s/blog_69398ed9010191jg.html

How to use:

1) The configuration file is in the Web-info class directory, or in the SRC directory of the Eclipse project

2) Problem: After modifying the configuration file, if the server is not restarted, will the configuration file take effect immediately? The result of your own use of the Tomcat test is yes, but it's a good idea to check further

Package Com.bobo.util;import Java.io.bufferedreader;import java.io.bufferedwriter;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.outputstreamwriter;import Java.util.hashmap;import java.util.Properties; Public classPropertiesutil {/** * @param args*/     Public Static voidMain (string[] args) {String fileName="test.properties"; Propertiesutil PU=NewPropertiesutil (); HashMap Map=NewHashMap (); Map.put (" Age"," -"); Map.put ("name","Leishao");        Pu.updateproperty (fileName, map); System. out. println (Pu.loadallproperties (fileName)); System. out. println (Pu.loadproperty (FileName,"name")); }     PublicHashmap<string, string>loadallproperties (String fileName) {InputStream is= Propertiesutil.class. getClassLoader (). getResourceAsStream (FileName); BufferedReader BR=NewBufferedReader (NewInputStreamReader ( is)); Properties PR=NewProperties (); Try{pr.load (BR); HashMap<string, string> HM =NewHashmap<string, string>();  for(Object s:pr.keyset ()) {Hm.put (s+"", Pr.getproperty (S +"")); }            returnHM; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }        return NULL; }     Publicstring LoadProperty (String fileName, String key) {InputStream is= Propertiesutil.class. getClassLoader (). getResourceAsStream (FileName); BufferedReader BR=NewBufferedReader (NewInputStreamReader ( is)); Properties PR=NewProperties (); Try{pr.load (BR); if(Pr.containskey (key)) {returnpr.Get(key) +""; }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }        return NULL; }     Public voidUpdateproperty (String fileName, hashmap<string,string>map) {InputStream is= Propertiesutil.class. getClassLoader (). getResourceAsStream (FileName); BufferedReader BR=NewBufferedReader (NewInputStreamReader ( is)); Properties PR=NewProperties (); String FilePath=propertiesutil.class. getClassLoader (). GetResource (FileName). GetFile (); Try{pr.load (BR);            Pr.putall (map); OutputStream out=NewFileOutputStream (FilePath); Pr.save ( out,""); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }             }    }

Java Web Engineering Read and modify configuration files

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.