Learning Goals:1, understanding the properties file, understand its meaning, will create the properties file correctly. 2, the Java.util.Properties class will be used to manipulate the properties file. 3, master the relative path, can correctly write a properties file relative path.I. Knowledge of the properties file1, the properties file is a text file 2, the properties file syntax There are two, one is a comment, a property configuration. Note: preceded by the # attribute configuration: Writes the configuration information for a property in the form of a key = value. 3, properties file a property configuration information value can be wrapped, but the key can not wrap. Value wrapping is denoted by "\". 4, the properties of the property configuration key values before and after the space will be ignored when parsing. 5, the properties file can have only keys and no value. You can also have a key and an equal sign without a value, but a property configuration cannot have a key anyway. For example, the following properties file: #正确的properties配置文件
Aaa=1\
11
B
bb = 222 #格式良好的properties文件
aaa=111
bbb=222second, the interpretation of Java.util.Properties class1, the Properties class hierarchy structure Java.lang.Object java.util.dictionary<k,v> java.util.hashtable<object,object> Java.util.Properties from a hierarchical perspective, the properties class implements the map interface because Hashtable implements the map interface, so the properties class is essentially a simple map container. In fact, the properties class itself represents an operation on a map structure. The properties file itself represents a collection of "key-value pairs". Therefore, the properties class belongs to the collection container's family and should create a container of properties before use, in effect creating a Properties object with no parameters. Then add "key-value pairs" to the inside by other means. 2, the properties file and the properties of the relationship through the properties file can populate the Properties class. You can also populate the Properties class with an XML file. You can load properties file information by an absolute path, or you can use a relative path to load it.Third, Practice1. Load properties file information in absolute relative path mode. 2. Persist the Properties object to a properties file or an XML file. 3. Modify and persist the properties file. Test code: Test the Properties file: #格式良好的properties文件
aaa=111
bbb=222 Test class: PackageStu
Importjava.io.*;
ImportJava.util.Properties;
/**
* Properties Class Test
* User:xiaohui
* Date:2008-11-4 21:04:54
*/
Publicclasstestproperties {
PublicStaticvoidMain (String args[])throwsIOException {
Testproperties ();
Test1 ();
}
Public StaticvoidTestproperties ()throwsIOException {
System.out.println ("------------testproperties-------------");
//Load the properties file into the input byte stream
InputStream is =NewFileInputStream ("D:\\myprojects\\lession4\\src\\stu\\ttt.properties");
//Create a Properties container
Properties prop =NewProperties ();
//Load properties file information from the stream
Prop.load (IS);
//Cyclic output configuration information
for(Object Key:prop.keySet ()) {
SYSTEM.OUT.PRINTLN (key +"="+ prop.get (key));
}
//define an output stream
OutputStream OS1 =NewFileOutputStream ("C:\\ttt.xml");
OutputStream OS2 =NewFileOutputStream ("C:\\ttt.properties");
//Export from Properties object to XML
Prop.storetoxml (OS1,"I exported the XML configuration file from Properties");
//Export properties file from Properties object
Prop.store (OS2,"I exported the XML configuration file from Properties");
Is.close ();
Os1.close ();
Os2.close ();
//Load configuration information from XML, populate the properties container
Prop.loadfromxml (NewFileInputStream ("C:\\ttt.xml"));
//Cyclic output configuration information
System.out.println ("I loaded configuration file information from the exported XML!" ");
for(Object Key:prop.keySet ()) {
SYSTEM.OUT.PRINTLN (key +"="+ prop.get (key));
}
//Modify Properties object and persist to a file
Prop.put ("Oh, oh","Quack");
OutputStream OS3 =NewFileOutputStream ("C:\\ttt1.xml");
Prop.storetoxml (OS3,"I exported the XML configuration file from Properties");
Os3.close ();
}
/**
* Load properties file in relative path mode
*
* @throws IOException
*/
PublicStatic voidTest1 ()throwsIOException {
System.out.println ("------------test1-------------");
Properties p =NewProperties ();
P.load (testproperties.class. getResourceAsStream ("/stu/ttt.properties"));
for(Object Key:p.keyset ()) {
SYSTEM.OUT.PRINTLN (key +"="+ p.get (key));
}
}
Run Result:------------testproperties-------------
bbb=222
aaa=111
I load configuration file information from exported XML!
bbb=222
aaa=111
------------test1-------------
bbb=222
aaa=111
Process finished with exit code 0 C: The file written below is as follows: hehe, all achieved the desired goal.
Java.util.Properties class