/*
練習使用Java.util.propertIEs類包來操作propertes及XML文件,通過store方法的調用可實現XML/propertIEs文件的相互保存轉化
*/
import Java.util.*;
import Java.io.*;
public class TestPropertes
{
public static void main(String[] args) {
Properties pp = new PropertIEs();
FileInputStream in = null;
FileInputStream XMLin = null;
FileOutputStream out = null;
PrintStream ps=null;
FileOutputStream XMLout =null;
try{
in = new FileInputStream("c:/Test.PropertIEs");
}catch(FileNotFoundException e)
{
System.out.println("propertIEs文件沒有找到");
}
try
{
pp.load(in);
}catch(IOException e)
{
System.out.println(e+"調用load方法時出現異常");
}
System.out.println(pp.getProperty("log4j.rootLogger"));
System.out.println(pp.getProperty("log4j.appender.A"));
System.out.println(pp.getProperty("log4j.appender.A1.layout"));
try
{
ps = new PrintStream("c:/TestMemo.txt");
out = new FileOutputStream("c:/Test.PropertIEs");
}catch(IOException e1)
{
System.out.println(e1+"PrintStream creat Error");
}
pp.list(ps); //將propertIEs文件用list方法讀取並存入ps文件中
//更改鍵值
Object o =pp.setProperty("log4j.rootLogger","DEBUG,B"); //更改對應的HASHTABLE中的值,實際上文件中的值沒變化
System.out.println(pp.getProperty("log4j.rootLogger"));
pp.list(ps); //將鍵值更改後的propertIEs文件用list方法讀取並存入ps文件中.list會自動以追回的方式從原文件尾寫入
System.out.println(o.getClass());
try
{
pp.store(out,""); //調用store方法保存文件.注意文件格式
}catch(IOException e)
{
System.out.println(e+"stroe方法調用錯誤");
}
try
{
xmlout = new FileOutputStream("c:/Test.XML");
}catch(IOException e)
{
}
try {
pp.storeToXML(xmlout,""); //將propertIEs文件保存為XML文檔
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//--練習用Java.util.propertIEs類讀取XML文檔
try{
xmlin = new FileInputStream("c:/Test.XML");
}catch(IOException e)
{
}
try {
pp.loadFromXML(XMLin);
} catch (InvalidPropertIEsFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(pp.getProperty("log4j.rootLogger"));
pp.setProperty("log4j.rootLogger","debug,a");
try {
pp.storeToXML(xmlout,""); //以追回方式添加到XML文檔
} catch (IOException e) {
e.printStackTrace();
}
try {
pp.store(out,"add file");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}