Import java. util .*;
Import java. Io .*;
/**
* Refer to http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index_eng.shtml </BR>
* Or http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index.shtml
*/
Public class loadproperties
{
Public static void main (string [] ARGs) throws exception {
Properties prop = new properties ();
// Load properties from configuration file
System. Out. println ("from properties file :");
Fileinputstream FCM = new fileinputstream ("sample. properties ");
Prop. Load (FS );
Prop. List (system. Out );
System. Out. println ("/nthe Foo property:" + Prop. getproperty ("foo "));
// Load properties from XML property file (Tiger new method)
System. Out. println ("from XML file :");
FCM = new fileinputstream ("sampleprops. xml ");
/**
* <PRE> the XML document must have the following doctype declaration:
* <! Doctype properties system "http://java.sun.com/dtd/properties.dtd">
* The DTD file:
* <? XML version = "1.0" encoding = "UTF-8"?>
* <! -- DTD for properties -->
* <! Element properties (comment ?, Entry *)>
* <! ATTLIST properties version CDATA # fixed" 1.0 ">
* <! Element comment (# pcdata)>
* <! Element entry (# pcdata)>
* <! ATTLIST Entry key CDATA # required>
* </PRE>
*/
Prop. loadfromxml (FCM );
Prop. List (system. Out );
System. Out. println ("/nthe Foo property:" + Prop. getproperty ("foo "));
// Generate an XML file
System. Out. println ("produce a XML file ");
Prop = new properties ();
Prop. setproperty ("One-Two", "buckle my shoe ");
Prop. setproperty ("three-four", "Shut the door ");
Prop. setproperty ("five-six", "pick up sticks ");
Prop. setproperty ("seven-eight", "lay them straight ");
Prop. setproperty ("nine-ten", "a big, fat success ");
Fileoutputstream Fos = new fileoutputstream ("rhyme. xml ");
/*
* Default encoding is UTF-8,
* If you need specify encoding,
* Use storetoxml (outputstream OS, string comment, string encoding) instead
*/
Prop. storetoxml (FOS, "rhyme"); // prop. storetoxml (FOS, "rhyme", "GBK ");
FOS. Close (); // The specified stream remains open after storetoxml () returns, so must close obviusly
/**
* The generated XML is as follows (DTD is described above ):
* <? XML version = "1.0" encoding = "UTF-8"?>
* <! Doctype properties system "http://java.sun.com/dtd/properties.dtd">
* <Properties>
* <Comment> rhyme </comment>
* <Entry key = "seven-eight"> lay them straight </entry>
* <Entry key = "five-six"> pick up sticks </entry>
* <Entry key = "nine-ten"> a big, fat week </entry>
* <Entry key = "three-four"> shut the door </entry>
* <Entry key = "One-Two"> buckle my shoe </entry>
* </Properties>
*/
}
}