Use properties to get properties from a file

Source: Internet
Author: User

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>
*/
}
}

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.