package testJavaProperty;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
public class TestProperty {
public static void main(String[] args){
//读取.property文件
// File pFile = new File("test.properties");
// FileInputStream pInStream = null;
//
// try {
// pInStream = new FileInputStream(pFile);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
//
// Properties p = new Properties();
// try {
// p.load(pInStream);
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// Enumeration
enu = p.propertyNames();//取出所有的key
// p.list(System.out);
// System.out.println("\n\n");
// while(enu.hasMoreElements()){
// String thisKey = (String)enu.nextElement();
// System.out.println("key="+thisKey);
// System.out.println("Value="+p.getProperty(thisKey));
// }
//写入.property文件
Properties p = new Properties();
p.setProperty("name", "China");
p.setProperty("location", "Asian");
p.setProperty("population", "14");
try {
PrintStream out = new PrintStream(new File("test.properties"));
p.list(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package testJavaProperty;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
public class TestPropertyXml {
public static void main(String[] args) {
//读取XML文件
// File pFile = new File("test.xml");
// FileInputStream pInStream = null;
// try {
// pInStream = new FileInputStream(pFile);
//
// Properties p = new Properties();
// p.loadFromXML(pInStream);
// p.list(System.out);
// } catch (IOException e) {
// e.printStackTrace();
// }
//写入XML文件
Properties p = new Properties();
p.setProperty("name", "China");
p.setProperty("location", "Asian");
p.setProperty("population", "14");
try {
PrintStream out = new PrintStream(new File("test.xml"));
p.storeToXML(out, "test");
} catch (IOException e) {
e.printStackTrace();
}
}
}
XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test</comment>
<entry key="location">Asian</entry>
<entry key="population">14</entry>
<entry key="name">China</entry>
</properties>