zoukankan      html  css  js  c++  java
  • 使用XStream是实现XML与Java对象的转换(5)--Object Stream

    八,Object Stream

     之前的例子我们都是直接输出Xml成为String类型或者从String中获得并解析Xml,现在我们要处理输入流和输出流!

    1,输出流(ObjectOutputStream)

    输出流测试程序如下:

    Java代码  
    1. package cn.tjpu.zhw.xml.xstream5;  
    2.    
    3. import java.io.IOException;  
    4. import java.io.ObjectOutputStream;  
    5.    
    6. import com.thoughtworks.xstream.XStream;  
    7.    
    8. public class OutMain {  
    9.    
    10.    public static void main(String[] args) throws IOException {  
    11.       //创建XStream对象  
    12.       XStream xstream = new XStream();  
    13.        
    14.       /*******1,序列化单个对象*******/  
    15.       System.out.println("*******1,序列化单个对象*******");  
    16.       xstream.toXML(new Person("张三"), System.out);  
    17.        
    18.       System.out.println();  
    19.       System.out.println();  
    20.        
    21.       /*******2,序列化一组对象*******/  
    22.       System.out.println("*******2,序列化一组对象*******");  
    23.       //将格式化后的xml输出到System.out中,root是根节点  
    24.       ObjectOutputStream oos = xstream.createObjectOutputStream(System.out,"root");  
    25.       oos.writeObject(new Person("张三"));  
    26.       oos.writeObject(new Person("李四"));  
    27.       oos.writeObject(new Integer(1));  
    28.       oos.writeObject(2);  
    29.       oos.writeObject(new Double(3));  
    30.       oos.writeObject(4d);  
    31.       oos.writeObject('c');  
    32.       oos.writeObject("这是一堆字符串!");  
    33.       //一定要关闭流  
    34.       oos.close();  
    35.    }  
    36. }  
    37.    
    38. class Person {  
    39.    public Person(String name){  
    40.       this.name = name;  
    41.    }  
    42.    private String name;  
    43.    
    44.    public String getName() {  
    45.       return name;  
    46.    }  
    47.    
    48.    public void setName(String name) {  
    49.       this.name = name;  
    50.    }  
    51.    
    52.    public String toString() {  
    53.       return "Person对象的name="+getName();  
    54.    }  
    55. }  

    运行结果:

    Java代码  
    1. *******1,序列化单个对象*******  
    2. <cn.tjpu.zhw.xml.xstream5.Person>  
    3.   <name>张三</name>  
    4. </cn.tjpu.zhw.xml.xstream5.Person>  
    5.    
    6. *******2,序列化一组对象*******  
    7. <root>  
    8.   <cn.tjpu.zhw.xml.xstream5.Person>  
    9.     <name>张三</name>  
    10.   </cn.tjpu.zhw.xml.xstream5.Person>  
    11.   <cn.tjpu.zhw.xml.xstream5.Person>  
    12.     <name>李四</name>  
    13.   </cn.tjpu.zhw.xml.xstream5.Person>  
    14.   <int>1</int>  
    15.   <int>2</int>  
    16.   <double>3.0</double>  
    17.   <double>4.0</double>  
    18.   <char>c</char>  
    19.   <string>这是一堆字符串!</string>  
    20. </root>  

     上面两个例子都是直接使用System.out,将XML输出到控制台。其实我们可以将其替换成我们喜欢的任何输出流,比如FileWriter等。

    2,输入流(ObjectInputStream)

    将如下内容写入D:/temp1.xml文件:

    Xml代码  
    1. <cn.tjpu.zhw.xml.xstream5.Person>  
    2.   <name>张三</name>  
    3. </cn.tjpu.zhw.xml.xstream5.Person>  

    将如下内容写入D:/temp2.xml文件:

    Xml代码  
    1. <root>  
    2.   <cn.tjpu.zhw.xml.xstream5.Person>  
    3.     <name>张三</name>  
    4.   </cn.tjpu.zhw.xml.xstream5.Person>  
    5.   <cn.tjpu.zhw.xml.xstream5.Person>  
    6.     <name>李四</name>  
    7.   </cn.tjpu.zhw.xml.xstream5.Person>  
    8.   <int>1</int>  
    9.   <int>2</int>  
    10.   <double>3.0</double>  
    11.   <double>4.0</double>  
    12.   <char>c</char>  
    13.   <string>这是一堆字符串!</string>  
    14. </root>  

    输入流测试程序如下:

    Java代码  
    1. package cn.tjpu.zhw.xml.xstream5;  
    2.    
    3. import java.io.EOFException;  
    4. import java.io.File;  
    5. import java.io.FileReader;  
    6. import java.io.IOException;  
    7. import java.io.ObjectInputStream;  
    8.    
    9. import com.thoughtworks.xstream.XStream;  
    10.    
    11. public class InMain {  
    12.    
    13.    public static void main(String[] args) throws IOException, ClassNotFoundException {  
    14.        
    15.       //创建XStream对象  
    16.       XStream xstream = new XStream();  
    17.        
    18.       /*******1,反序列化一个对象*******/  
    19.       FileReader reader1 = new FileReader(new File("D:/temp1.xml"));  
    20.       Person pp = (Person)xstream.fromXML(reader1);  
    21.       System.out.println("*******1,反序列化一个对象*******");  
    22.       System.out.println("pp="+pp);  
    23.        
    24.        
    25.       System.out.println();  
    26.       System.out.println();  
    27.        
    28.       /*******1,反序列化一组对象*******/  
    29.       FileReader reader2 = new FileReader(new File("D:/temp2.xml"));  
    30.       ObjectInputStream ois = xstream.createObjectInputStream(reader2);  
    31.        
    32.       Person p1 = (Person)ois.readObject();  
    33.       System.out.println("p1="+p1);  
    34.        
    35.       Person p2 = (Person)ois.readObject();  
    36.       System.out.println("p2="+p2);  
    37.        
    38.       int i1 = (Integer)ois.readObject();  
    39.       System.out.println("i1="+i1);  
    40.        
    41.       int i2 = (Integer)ois.readObject();  
    42.       System.out.println("i2="+i2);  
    43.        
    44.       double d1 = (Double)ois.readObject();  
    45.       System.out.println("d1="+d1);  
    46.        
    47.       double d2 = (Double)ois.readObject();  
    48.       System.out.println("d2="+d2);  
    49.        
    50.       char ch = (Character)ois.readObject();  
    51.       System.out.println("ch="+ch);  
    52.        
    53.       String str = (String)ois.readObject();  
    54.       System.out.println("str="+str);  
    55.        
    56.       System.out.println("******异常捕获******");  
    57.       //发生异常  
    58.       try {  
    59.         ois.readObject();  
    60.       } catch (EOFException e) {  
    61.         System.out.println("因为已经没有数据了,再次读取时,就会发生EOFException异常");  
    62.       }  
    63.    }  
    64.    
    65. }  

    运行结果:

    Java代码  
    1. *******1,反序列化一个对象*******  
    2. pp=Person对象的name=张三  
    3.    
    4.    
    5. p1=Person对象的name=张三  
    6. p2=Person对象的name=李四  
    7. i1=1  
    8. i2=2  
    9. d1=3.0  
    10. d2=4.0  
    11. ch=c  
    12. str=这是一堆字符串!  
    13. ******异常捕获******  
    14. 因为已经没有数据了,再次读取时,就会发生EOFException异常 
  • 相关阅读:
    第三天 python的初始编码,基本数据类型(int,str,bool),字符串的操作
    while循环,格式化输出,运算符,while...else...
    python的种类,变量,常量,基础数据类型,input,if条件语句
    JavaScript 之 web API
    传输层上的TCP和UDP
    应用层上的协议HTTP
    计算机网络通信
    grid布局
    使用classList和dataset实现tab切换
    JS之跨域
  • 原文地址:https://www.cnblogs.com/eer123/p/7894914.html
Copyright © 2011-2022 走看看