9.序列化反序列化链表

来源:互联网 发布:淘宝哪家手机壳质量好 编辑:程序博客网 时间:2024/06/14 18:40
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Random;class Link implements Serializable{/** * 简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。 * 在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与 * 本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的, * 可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException) *  * 问题一:假设有A端和B端,如果2处的serialVersionUID不一致,会产生什么错误呢?     1)先执行测试类SerialTest,然后修改serialVersion值(或注释掉serialVersion并编译),   再执行测试类DeserialTest,报错:java.io.InvalidClassException: com.test.serializable.Serial; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 112)A端和B端都没显示的写serialVersionUID,实体类没有改动(如果class文件(类名,方法明等)没有发生变化(增加空格,换行,增加注释,等等),).序列化,反序列化正常.  * 问题二:假设2处serialVersionUID一致,如果A端增加一个字段,B端不变,会是什么情况呢? *  * 序列化,反序列化正常,A端增加的字段丢失(被B端忽略). *      * * 问题三:假设2处serialVersionUID一致,如果B段增加一个字段,A端不变,会是什么情况呢? *  * 序列化,反序列化正常,B端新增加的int字段被赋予了默认值0. *  * * 问题四:假设2处serialVersionUID一致,如果A端减少一个字段,B端不变,会是什么情况呢? *  * 序列化,反序列化正常,B端字段多余A端,B端多出的字段被赋予对应类型的默认值) *  * * 问题五:假设2处serialVersionUID一致,如果B端减少一个字段,A端不变,会是什么情况呢? *  * 与问题二类似,序列化,反序列化正常,B端字段少于A端,A端多的字段值丢失(被B端忽略). */private static final long serialVersionUID = 1L;class Node implements Serializable{/** *  */private static final long serialVersionUID = 1L;int data;Node next;public Node(int data){this.data = data;this.next = null;}public int getData() {return data;}public void setData(int data) {this.data = data;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}@Overridepublic String toString() {return "Node [data=" + data + "]";}}Node head;int size = 0;public Link(){head = new Node(0);head.setNext(null);}public void insertHead(int data){Node newNode = new Node(data);newNode.setNext(head.getNext());head.setNext(newNode);size++;}public void insertTail(int data){Node newNode = new Node(data);Node before = head;Node cur = head.getNext();while (cur != null){before = cur;cur = cur.getNext();}before.setNext(newNode);size++;}public Node get(int index){if (index < 0 || index > size)return null;Node cur = head.getNext();for (int i = 0; i < index; ++i){cur = cur.getNext();}return cur;}public void set(int data, int index){if (index < 0 || index > size)return;Node cur = head.getNext();for (int i = 0; i < index; ++i){cur = cur.getNext();}cur.setData(data);}public int size(){return size;}private void writeObject(java.io.ObjectOutputStream s){try {s.writeInt(this.size);  //先写入链表节点个数for (Node x = head.getNext(); x != null; x = x.getNext()){s.writeObject(x.getData());  //依次写入节点数据}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void readObject(java.io.ObjectInputStream s){try {int size = s.readInt();  //先读取节点节点个数this.size = size;this.head = new Node(0);for (int i = 0; i < size; ++i){int data = (int) s.readObject();  //依次读取节点数据构造节点Node newNode = new Node(data);Node cur = this.head;while (cur.getNext() != null){cur = cur.getNext();}cur.setNext(newNode);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}void LinkLast(int data){Node l = head;Node newNode = new Node(data);while (l.getNext() != null){l = l.getNext();}l.setNext(newNode);}public String toString(){StringBuilder sb = new StringBuilder("");Node cur = head.getNext();while (cur != null){sb.append(cur + "\n");cur = cur.getNext();}return sb.toString();}}public class TestTest {public static void main(String[] args) {// TODO Auto-generated method stubtry {Link link = new Link();for (int i = 1; i <= 10; ++i){link.insertTail(i);}ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("link.txt")));out.writeObject(link);out.close();ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("link.txt")));Link l =  (Link) in.readObject();in.close();System.out.println("l:");System.out.println(l);l.set(100, 5);System.out.println(l);System.out.println("link");System.out.println(link);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

0 0
原创粉丝点击