In projects, we often write configuration files in xml format, and give the xml file path in the properties file.
For reading properties files, the propertyUtil tool class is given here.
Before this, please note: After Java Properties loads the properties file, there is no guarantee that the output order will be consistent with the file, because Properties are inherited from Hashtable, and key/value are directly stored in Hashtable. , and Hashtable does not guarantee the order of entry and exit.
Solution: Inherit the java.util.Properties class and use LinkedHashSet to save all its keys.
The OrderedProperties class is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.test;import java.util.Collections;import java.util.Enumeration;import java.util.LinkedHashSet;import java.util.Properties;import java.util.Set; public class OrderedProperties extends Properties{ private static final long serialVersionUID = 1L; private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
public Enumeration<Object> keys() { return Collections.<Object> enumeration(keys);
}
public Object put(Object key, Object value) {
keys.add(key); return super.put(key, value);
}
public Set<Object> keySet() { return keys;
}
public Set<String> stringPropertyNames() {
Set<String> set = new LinkedHashSet<String>();
for (Object key : this.keys) {
set.add((String) key);
}
return set;
}
}
|
Copy after login
The main method that reads configuration file properties and parses xml:
1 2 3 4 5 6 | package com.test; public class T { public static void main(String[] args) {
configureFileFw.parseConfigXmlFile(WorkConfigureFile);
}
}
|
Copy after login
The propertyUtil class is as follows


1 2 3 4 5 6 7 8 9 10 | 1 package com.test; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 9 public class propertityUtil { 10 11 14 private OrderedProperties prop = null; 15 16 19 private String CONFIGPATH = "project-config.properties" ; 20 21 24 public propertityUtil() { 25 init(); 26 } 27 28
34 public propertityUtil(OrderedProperties prop) { 35 this.prop = prop; 36 } 37 38
43 public propertityUtil(String conf) { 44 this.CONFIGPATH = conf; 45 init(CONFIGPATH); 46 } 47 48
53 public OrderedProperties getProp() { 54 return prop; 55 } 56 57
62 public void setProp(OrderedProperties prop) { 63 this.prop = prop; 64 } 65 66
72 public static propertityUtil getInstance(String configpath) { 73 return new propertityUtil(configpath); 74 } 75 76
81 private void init(String configpath) { 82 this.CONFIGPATH = configpath; 83 init(); 84 } 85 86 89 private void init() { 90 if (this.CONFIGPATH == null || this.CONFIGPATH.length() == 0) { 91 System.out.println( "No configFile" ); 92 } else { 93 94 String srcDir = System.getProperty( "user.dir" ); 95 srcDir = srcDir + "/resources/properties/" + this.CONFIGPATH; 96 prop = new OrderedProperties(); 97 FileInputStream fin = null; 98 try { 99 fin = new FileInputStream(srcDir);100 prop.load(fin);101 } catch (FileNotFoundException e2) {102 e2.printStackTrace();103 } catch (IOException e2) {104 e2.printStackTrace();105 }106 107
120 * @param propName121 * @return122 */123 public String getProperty(String propName) {124 return getProperty(propName, "" );125 }126 127
133 public String getProperty(String propName, String defValue) {134 if (prop == null)135 init();136 String s = defValue;137 try {138 s = prop.getProperty(propName);139 if (s == null) {140 s = defValue;141 } else {142 s = s.trim();143 }144 } catch (Exception e) {145 e.printStackTrace();146 }147 return s;148 }149 150
156 public void setProperty(String propName, String value) {157 if (prop == null)158 init();159 try {160 prop.setProperty(propName, value);161 storeProperty();162 } catch (Exception e) {163 e.printStackTrace();164 }165 }166 167 170 private void storeProperty() {171 try {172 String path = prop.getProperty( "path" );173 OutputStream out = new FileOutputStream(path);174 prop.store(out, " -- sl Soft Config File -- " );175 out.close();176 } catch (Exception ioe) {177 ioe.printStackTrace();178 }179 }180 }
|
Copy after login
View Code
xml parsing class


1 | 1 package com.test; 2 3 import java.io.File; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import org.dom4j.Document; 8 import org.dom4j.Element; 9 import org.dom4j.io.SAXReader; 10 11 16 public class ConfigureFileFw { 17 18 21 private static Map<String, Map<String, String>> workAttributes = new HashMap<String, Map<String, String>>(); 22 23 private Document document = null; 24 25 private Element rootElement = null; 26 27 31 public Map<String, Map<String, String>> getWorkAttributes() { 32 return workAttributes; 33 } 34 38 public Map<String, String> getWorkAttributesByWorkId(String workId) { 39 return workAttributes.get(workId); 40 } 41 42 47 public String hasWorkId(String workId) { 48
|
Copy after login
View Code
The above is the detailed content of The main method that reads configuration file properties and parses xml. For more information, please follow other related articles on the PHP Chinese website!