import java.util.*;
import java.io.*;
/**
* refer to http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index_eng.shtml
* or http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index.shtml
*/
public class LoadProperties
{
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
//load properties from configuration file
System.out.println("From properties file:");
FileInputStream fis = new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
//load properties from xml property file(Tiger new method)
System.out.println("From xml file:");
fis =new FileInputStream("sampleprops.xml");
/**
*
The XML document must have the following DOCTYPE declaration:
*
*the dtd file :
*
*
*
*
*
*
*
*
*/
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
//生成xml文件
System.out.println("produce a xml file");
prop = new Properties();
prop.setProperty("one-two", "buckle my shoe");
prop.setProperty("three-four", "shut the door");
prop.setProperty("five-six", "pick up sticks");
prop.setProperty("seven-eight", "lay them straight");
prop.setProperty("nine-ten", "a big, fat hen");
FileOutputStream fos = new FileOutputStream("rhyme.xml");
/*
*default encoding is UTF-8,
* if you need specify encoding,
* use storeToXML(OutputStream os,String comment,String encoding) instead
*/
prop.storeToXML(fos, "Rhyme");//prop.storeToXML(fos, "Rhyme","GBK");
fos.close();//The specified stream remains open after storeToXML() returns,so must close obviously
/**
*生成的xml如下(DTD如上所述):
*
*
*
*
*
*
*
*
*
*
*/
}
}