八,Object Stream
之前的例子我们都是直接输出Xml成为String类型或者从String中获得并解析Xml,现在我们要处理输入流和输出流!
1,输出流(ObjectOutputStream)
输出流测试程序如下:
Java代码
- package cn.tjpu.zhw.xml.xstream5;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import com.thoughtworks.xstream.XStream;
- public class OutMain {
- public static void main(String[] args) throws IOException {
- //创建XStream对象
- XStream xstream = new XStream();
- /*******1,序列化单个对象*******/
- System.out.println("*******1,序列化单个对象*******");
- xstream.toXML(new Person("张三"), System.out);
- System.out.println();
- System.out.println();
- /*******2,序列化一组对象*******/
- System.out.println("*******2,序列化一组对象*******");
- //将格式化后的xml输出到System.out中,root是根节点
- ObjectOutputStream oos = xstream.createObjectOutputStream(System.out,"root");
- oos.writeObject(new Person("张三"));
- oos.writeObject(new Person("李四"));
- oos.writeObject(new Integer(1));
- oos.writeObject(2);
- oos.writeObject(new Double(3));
- oos.writeObject(4d);
- oos.writeObject(‘c‘);
- oos.writeObject("这是一堆字符串!");
- //一定要关闭流
- oos.close();
- }
- }
- class Person {
- public Person(String name){
- this.name = name;
- }
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String toString() {
- return "Person对象的name="+getName();
- }
- }
运行结果:
Java代码
- *******1,序列化单个对象*******
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>张三</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- *******2,序列化一组对象*******
- <root>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>张三</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>李四</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <int>1</int>
- <int>2</int>
- <double>3.0</double>
- <double>4.0</double>
- <char>c</char>
- <string>这是一堆字符串!</string>
- </root>
上面两个例子都是直接使用System.out,将XML输出到控制台。其实我们可以将其替换成我们喜欢的任何输出流,比如FileWriter等。
2,输入流(ObjectInputStream)
将如下内容写入D:/temp1.xml文件:
Xml代码
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>张三</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
将如下内容写入D:/temp2.xml文件:
Xml代码
- <root>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>张三</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name>李四</name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <int>1</int>
- <int>2</int>
- <double>3.0</double>
- <double>4.0</double>
- <char>c</char>
- <string>这是一堆字符串!</string>
- </root>
输入流测试程序如下:
Java代码
- package cn.tjpu.zhw.xml.xstream5;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import com.thoughtworks.xstream.XStream;
- public class InMain {
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- //创建XStream对象
- XStream xstream = new XStream();
- /*******1,反序列化一个对象*******/
- FileReader reader1 = new FileReader(new File("D:/temp1.xml"));
- Person pp = (Person)xstream.fromXML(reader1);
- System.out.println("*******1,反序列化一个对象*******");
- System.out.println("pp="+pp);
- System.out.println();
- System.out.println();
- /*******1,反序列化一组对象*******/
- FileReader reader2 = new FileReader(new File("D:/temp2.xml"));
- ObjectInputStream ois = xstream.createObjectInputStream(reader2);
- Person p1 = (Person)ois.readObject();
- System.out.println("p1="+p1);
- Person p2 = (Person)ois.readObject();
- System.out.println("p2="+p2);
- int i1 = (Integer)ois.readObject();
- System.out.println("i1="+i1);
- int i2 = (Integer)ois.readObject();
- System.out.println("i2="+i2);
- double d1 = (Double)ois.readObject();
- System.out.println("d1="+d1);
- double d2 = (Double)ois.readObject();
- System.out.println("d2="+d2);
- char ch = (Character)ois.readObject();
- System.out.println("ch="+ch);
- String str = (String)ois.readObject();
- System.out.println("str="+str);
- System.out.println("******异常捕获******");
- //发生异常
- try {
- ois.readObject();
- } catch (EOFException e) {
- System.out.println("因为已经没有数据了,再次读取时,就会发生EOFException异常");
- }
- }
- }
运行结果:
Java代码
- *******1,反序列化一个对象*******
- pp=Person对象的name=张三
- p1=Person对象的name=张三
- p2=Person对象的name=李四
- i1=1
- i2=2
- d1=3.0
- d2=4.0
- ch=c
- str=这是一堆字符串!
- ******异常捕获******
- 因为已经没有数据了,再次读取时,就会发生EOFException异常
时间: 2024-11-05 21:59:55