Properties对文件操作问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Properties对文件操作问题相关的知识,希望对你有一定的参考价值。

我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(fos, "1111");
prop2.clear();
fos.close();


这样写只能覆盖原来的key和value,有没有什么办法可以在不改变原有值得情况下继续往下写xml的内容?

扬自飞扬 我不太明白,Object setProperty(String key, String value)这个是怎么用?

参考技术A 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);
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");
/**
*<pre>The XML document must have the following DOCTYPE declaration:
*<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
*the dtd file :
*<?xml version="1.0" encoding="UTF-8"?>
*<!-- DTD for properties -->
*<!ELEMENT properties ( comment?, entry* ) >
*<!ATTLIST properties version CDATA #FIXED "1.0">
*<!ELEMENT comment (#PCDATA) >
*<!ELEMENT entry (#PCDATA) >
*<!ATTLIST entry key CDATA #REQUIRED>
*</pre>
*/
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如上所述):
*<?xml version="1.0" encoding="UTF-8"?>
*<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
*<properties>
*<comment>Rhyme</comment>
*<entry key="seven-eight">lay them straight</entry>
*<entry key="five-six">pick up sticks</entry>
*<entry key="nine-ten">a big, fat hen</entry>
*<entry key="three-four">shut the door</entry>
*<entry key="one-two">buckle my shoe</entry>
*</properties>
*/

参考技术B 追加可以修改这个FileOutputStream fos=new FileOutputStream("d:\\craterXML.xml",true); 这样你试下吧。本回答被提问者采纳 参考技术C setProperty(String key, String value)就是将值保存,并提供一个键,以后可以通过那个键再把值取出来,取值方法为 Object getProperty(String key). 参考技术D 从算法来说,jdom比dom和sax都好,为什么不用呢。 第5个回答  2008-05-09 先load回来,然后set,最后store

以上是关于Properties对文件操作问题的主要内容,如果未能解决你的问题,请参考以下文章

对Java配置文件Properties的读取写入与更新操作

c++对properties配置文件操作工具类

java properties配置文件操作

properties配置文件的基本操作

属性文件操作之Properties与ResourceBundle

properties配置文件读取操作总结java笔记