写xml
package xml;
import java.io.IOException;
import java.io.File;
import java.io.PrintStream;
import java.util.Properties;
public class propertiesTOXML {
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id","dean");
p.setProperty("password","123456");
try{
PrintStream fW = new PrintStream(new File("e:\\test1.xml"));
p.storeToXML(fW,"test");
} catch (IOException e) {
e.printStackTrace();
}
}
}
读xml
package xml;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;
public class propertiesLoadXML {
public static void main(String[] args) {
File pFile = new File("e:\\test1.xml"); // properties文件放在e盘下(windows)
FileInputStream pInStream = null;
try {
pInStream = new FileInputStream(pFile);
Properties p = new Properties();
p.loadFromXML(pInStream);
//p.list(System.out);
//另一个方法遍历
Iterator iter = p.keySet().iterator();
while(iter.hasNext()) {
String key = (String)iter.next();
String value = p.getProperty(key);
String value2 = p.getProperty("30");
System.out.println(key+"="+value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}