问题是每次执行main方法时,a.xml的旧内容都丢失了,被新的替换掉了。如何在不丢失之前信息的情况下将内容附加到 a.xml 文件?
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
XStream xs = new XStream(new DomDriver());
Foo f = new Foo(1, "booo", new Bar(42));
PrintWriter pw = new PrintWriter("a.xml");
xs.toXML(f,pw);
}
}
public class Bar {
public int id;
public Bar(int id) {
this.id = id;
}
}
public class Foo {
public int a;
public String b;
public Bar boo;
public Foo(int a, String b, Bar c) {
this.a = a;
this.b = b;
this.boo = c;
}
}