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