1. Methods of basic Load properties
Sample Properties File
Sample.properties
Foo=bar
Fu=baz
Sample for loading properties program
Import Java.io.FileInputStream;
Import java.util.Properties;
public class Loadsampleproperties {
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"));
}
}
The output results are as follows:
--Listing properties--
Fu=baz
Foo=bar
The Foo Property:bar
2. Loading properties from XML
here is the properties DTD list:
<?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 >
Sample XML Properties 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
from an XML file Load properties of sample program
Import Java.io.FileInputStream;
Import java.util.Properties;
public class Loadsamplexml {
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"));
}
}
The results of the output are the same
You 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
3. Update the attribute values in the XML file
Sample program
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.Properties;
public class Updatesamplexml {
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"));
Prop.setproperty ("foo", "Hello world!");
Prop.setproperty ("New-name", "New-value");
FileOutputStream fos = new FileOutputStream ("Props/sample.xml");
Prop.storetoxml (FOS, "Store Sample");
Fos.close ();
FIS = new FileInputStream ("Props/sample.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("/nthe foo property:" + Prop.getproperty ("foo"));
}
}
The example above loads the attributes in Sample.xml, updates the value of the Foo property, adds the New-name attribute, invokes the Storetoxml () method to save it to the original file, and changes the comment to store Sample
sample.xml after the execution of the program the contents are as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >
<properties>
<comment>store sample</comment>
<entry key= "New-name" >new-value</entry>
<entry key= "Fu" >baz</entry>
<entry key= "foo" >hello world!</entry>
</properties>