Java.util.Properties is a mapping of configuration files such as properties. Two types of Key-value and XML types are supported. Key-value type of configuration file is roughly as long as this: Copy code # test environment configuration: Platform Path configuration Jstrd_home=d:/tms2006/webapp/tms2006/web-inf/dbport = Localhostdatabasename = Myddbusername = root Copy code # starts with a comment line, and properties ignores the comment. Only key is allowed without value. For example: Copy code # test environment configuration: Platform Path configuration jstrd_home=d:/tms2006/webapp/tms2006/web-inf/dbport = databasename Copy code in this case, Value is set to NULL. The properties class implements the map interface, so it is clear that he is using a map to store key-value data, so it is also destined to deposit data is unordered, this point needs attention. The get corresponds to value only by means of key. For key-value This configuration file, the Load method can be directly mapped into a map, very simple and easy to use. This configuration file is also our most important configuration file, the use of properties to read this kind of file to memory line of code on the Erco, more powerful than their own parsing, this is great. The approximate code to read the configuration file is as follows: Copy Code 1 public class Loadsample {2 public static void Main (String args[]) throws Exception {3 Properties PR op = new Properties (); 4 FileInputStream FIS = 5 new FileInputStream ("Sample.properties"); 6 prop.load (FIS); 7 prop.list (System.out); 8 System.out.println ("\nthe foo property:" + 9 Prop.getproperty ("foo")); 10} 11} The Load method on line sixth of the replication code directly produces a memory map, and the Nineth line gets to the corresponding value, which is quick and easy. Here the seventh line of the list method is an output method, which is output to the console, can also output to a file, etc., can be implemented withinWrite to the configuration file. For example: Copy code 1//through the list method to write properties to the Properties file 2 import java.io.IOException; 3 Import Java.io.File; 4 Import Java.io.FileInputStream; 5 Import Java.io.PrintStream; 6 Import java.util.Properties; 7 8 public class Test {9 public static void Main (string[] args) {Ten properties P = new properties (); P.setproperty (" ID "," Dean "), P.setproperty (" Password "," 123456 "), try{16 printstream FW = new PrintStream (" e:\\ Test1.properties ")); P.list (FW); catch (IOException e) {e.printstacktrace (); 19 20}21}22} Copy the code so that the properties object in memory can be written to the file. Another form of configuration is XML, which is less common than the previous one. The configuration file format in the form of XML is roughly the same: Copy code copy code read XML configuration is no different from reading the KV configuration, is to replace load with XML corresponding to the LoadFromXML method, the code is roughly the same: Copy Code 1 public class Loadsamplexml {2 public static void Main (String args[]) throws Exception {3 Properties prop = new Properties (); 4 FileInputStream FIS = 5 new FileInputStream ("Sampleprops.xml"); 6 prop.loadfromxml (FIS); 7 prop.list (System.out); 8 System.out.println ("\nthe foo property:" + 9 Prop.getpropErty ("foo")); 10} 11} The copy code writes the In-memory properties object to the XML file and is similar to the above, which is to change the list method to the Storetoxml method of the XML counterpart. The code is roughly like this: Copy code 1 import java.io.IOException; 2 Import Java.io.File; 3 Import Java.io.FileInputStream; 4 Import Java.io.PrintStream; 5 Import java.util.Properties; 6 7 public class Test {8 public static void Main (string[] args) {9 Properties P = new properties (); P.setproperty ("id" , "Dean"); P.setproperty ("Password", "123456"); try{14 printstream FW = new PrintStream (New File ("E:\\test1.xml")) ; P.storetoxml (FW, "test"), and the catch (IOException e) {e.printstacktrace (); 18}19}20}21
Java Read configuration file