使用XStream是實現XML與Java對象的轉換(5)--Object Stream
阿新 • • 發佈:2017-11-25
控制 () oid dex 關閉 ava -o setname otf
八,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異常
使用XStream是實現XML與Java對象的轉換(5)--Object Stream