Properties对文件操作问题

来源:百度知道 编辑:UC知道 时间:2024/09/22 19:34:07
我JavaBean中使用Properties对xml写键、值对,可怎么进行修改和追加后面的键、值对呀?
我不想用 JDOM、SAX。代码太多了 我的xml非常简单,不需要太复杂的操作
望高人赐教
XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>1111</comment>
<entry key="testStr2">11</entry>
<entry key="testStr">11</entry>
</properties>

程序的代码:
public class XmlFlie {
public static void xml(String textStr,String textStr2)throws Exception{
Properties prop2 = new Properties();
prop2.setProperty("testStr", textStr);
prop2.setProperty("testStr2", textStr2);
FileOutputStream fos=new FileOutputStream("d:\\craterXML.xml");
prop2.storeToXML(fo

追加可以修改这个FileOutputStream fos=new FileOutputStream("d:\\craterXML.xml",true); 这样你试下吧。

import java.util.*;
import java.io.*;

/**
* refer to http://www-900.ibm.com/developerWorks/cn/java/j-tiger02254/index_eng.shtml </br>
* 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);
pr