The Java.util.Properties class is updated in the xml| load JDK1.5 (code-named Tiger), providing a simple way to read and write Key-value attributes from an XML file: LoadFromXML () and Storetoxml ()
1, basic load properties of the method
L Sample Properties File: Sample.properties
Foo=bar
Fu=baz
L Loading properties of sample program
public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Props/sample.properties");
Prop.load (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L OUTPUT The following results:
--Listing properties--
Fu=baz
Foo=bar
The Foo Property:bar
2. Loading properties from XML
L The following is a list of properties DTDs:
<?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 >
l Sample XML attribute file: Sample.xml (conforming to the properties DTD above)
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >
<properties>
<comment>Hello</comment>
<entry key= "foo" >bar</entry>
<entry key= "Fu" >baz</entry>
</properties>
L <entry> Tags Specify a property, the property name is specified by the key property, and the contents of the <entry> tag specify the property value
L <comment> tags can be used to specify annotations
L A sample program that loads attributes from an XML file
public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Props/sample.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L output is the same as the result
L can see that the method is simple: Use an XML file to save the property, and use the LoadFromXML () method instead of the original load () method to load the attribute in the XML file
FIS = new FileInputStream ("Props/sample.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L The example above loads the attributes in Sample.xml, updates the value of the Foo property, adds the New-name property, invokes the Storetoxml () method to save it to the original file, and changes the comment to store Sample
The contents of the Sample.xml following the implementation of the program are as follows:
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.