Java之对象序列化与反序列化

来源:互联网 发布:除尘风道计算软件 编辑:程序博客网 时间:2024/05/10 20:43

1.将对象转换为字节流保存起来,并在以后还原这个对象,这种机制叫做对象序列化

2.将一个对象保存到永久存储设备上称为持久化

3.一个类若想被序列化,则需要实现java,io.Serializable接口,该接口中没有定义任一方法,是一个标志性接口(Mark Interface),当一个类实现了该接口,就说明该类是可以序列化的。

4.在序列化时,static变量是无法序列化的;如果A包含了对B的引用,那么在序列化A的时候也会将B一并地序列化;如果此时A可以序列化,B无法序列化,那么在序列化A的时候就会发生异常,这时就需要将对B的引用设为transient,该关键字表示变量不会被序列化。

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;/** * 对象序列化与反序列化 * @author c */public class SerializableTest1{public static void main(String[] args) throws Exception{Person p1 = new Person(1, "张三", 21);Person p2 = new Person(2, "李四", 22);Person p3 = new Person(3, "王五", 20);FileOutputStream fos = new FileOutputStream("E:\\eclipse4.2\\JavaIo\\doc\\Person.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(p1);oos.writeObject(p2);oos.writeObject(p3);oos.close();System.out.println("-----------------");FileInputStream fis = new FileInputStream("E:\\eclipse4.2\\JavaIo\\doc\\Person.txt");ObjectInputStream ois = new ObjectInputStream(fis);Person p = null;for (int i = 0; i < 3; i++){p = (Person)ois.readObject();System.out.println("id = " + p.id + ",name = " + p.name + ",int = " + p.age);}ois.close();}}class Person implements Serializable {/* * 加Static或transient关键字则不能被存储 */int id;String name;int age;public Person(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}}

5.利用对象的序列化和反序列化,可以实现对对象的深克隆当克隆的对象只有基本类型,不含引用类型时,可以用浅克隆实现,当克隆的对象含有引用类型时,必须使用深克隆实现

/* * 对象的浅克隆
 *1.实现Cloneable接口
 *2.重写clone()方法
 *3.在clone()方法中调用super.clone() 方法 */public class ShallowClone {public static void main(String[] args) {Student s1 = new Student();s1.setId(100);s1.setName("鸣人");System.out.println("克隆前的id:" + s1.getId());System.out.println("克隆前的name:" + s1.getName());try {Student s2 = (Student) s1.clone();System.out.println("克隆后的id:" + s2.getId());System.out.println("克隆后的name:" + s2.getName());} catch (CloneNotSupportedException e) {e.printStackTrace();}}}class Student implements Cloneable {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic Object clone() throws CloneNotSupportedException {Student s = (Student) super.clone();return s;}}


package com.deepclone;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;/* * 利用对象序列化实现对象的深克隆
 *1.实现Serializable接口
 *2.利用ObjectOutputStream的writeObject方法将要克隆的对象序列化
 *3.利用ObjectInputStream的readObject方法将克隆得到的对象反序列化 */public class DeepCopy {public static void main(String[] args) {Father father = new Father();father.setAge(20);father.setName("四代火影");Son son = new Son();son.setAge(18);son.setName("鸣人");son.setFather(father);System.out.println("克隆前son的age:" + son.getAge());System.out.println("克隆前son的name:" + son.getName());System.out.println("克隆前fathor的age:" + son.getFather().getAge());System.out.println("克隆前fathor的name:" + son.getFather().getName());System.out.println("---------------------------");Son son2 = (Son)son.copyObject();System.out.println("克隆前son的age:" + son2.getAge());System.out.println("克隆前son的name:" + son2.getName());System.out.println("克隆前fathor的age:" + son2.getFather().getAge());System.out.println("克隆前fathor的name:" + son2.getFather().getName());System.out.println("---------------------------");son2.getFather().setAge(21);son2.getFather().setName("波分水门");System.out.println("克隆前fathor的age:" + son.getFather().getAge());System.out.println("克隆前fathor的name:" + son.getFather().getName());System.out.println("克隆前fathor的age:" + son2.getFather().getAge());System.out.println("克隆前fathor的name:" + son2.getFather().getName());}}class Father implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}class Son implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age;private Father father;public Father getFather() {return father;}public void setFather(Father father) {this.father = father;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}/* * 实现对象的序列化与反序列化 */public Object copyObject() {ByteArrayOutputStream baos = new ByteArrayOutputStream();try {ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(this);//对象序列化} catch (IOException e) {e.printStackTrace();}ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());Object object = null;try {ObjectInputStream ois = new ObjectInputStream(bais);object = ois.readObject();//对象反序列化} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}return object;}}



原创粉丝点击