首先下载XStream-X.X.X.jar包,然后加载到类路径下。例子代码如下:
import
java.io.
*
;
import
java.beans.
*
;
import
com.thoughtworks.xstream.
*
;
import
com.thoughtworks.xstream.io.xml.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Test
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) throws IOException, ClassNotFoundException ...{
// TODO Auto-generated method stub
jdkReader();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void xstreamWriter() throws IOException...{
FileWriter fw = new FileWriter("out.xml",true);
XStream xstream = new XStream();
ObjectOutputStream out = xstream.createObjectOutputStream(fw);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
out.writeObject(new Person("Joe", "Walnes"));
out.writeObject(new Person("Someone", "Else"));
out.writeObject("hello");
out.writeInt(12345);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
out.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void xstreamReader() throws IOException, ClassNotFoundException...{
FileReader fr = new FileReader("out.xml");
XStream xstream = new XStream(new DomDriver());
ObjectInputStream in = xstream.createObjectInputStream(fr);
Person a = (Person)in.readObject();
Person b = (Person)in.readObject();
String c = (String)in.readObject();
int d = in.readInt();
System.out.println(a.getFirstName());
System.out.println(b.getFirstName());
System.out.println(c);
System.out.println(d);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void jdkWriter() throws FileNotFoundException...{
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Test.xml")));
e.writeObject(new Person("Hello,","world"));
e.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void jdkReader() throws FileNotFoundException...{
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream("Test.xml")));
Person p = (Person)d.readObject();
d.close();
System.out.println(p.getFirstName());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Person
...
{
private String firstName;
private String lastName;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Person()...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Person(String afirstName,String alastName)...{
firstName = afirstName;
lastName = alastName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getFirstName() ...{
return firstName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setFirstName(String firstName) ...{
this.firstName = firstName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getLastName() ...{
return lastName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setLastName(String lastName) ...{
this.lastName = lastName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
jdk提供的方法只能对JavaBean对象进行序列化,所以具有局限性,而且只能序列化xxxSetter(),xxxGetter,removeXXX()方法。