package chapter6; import java.io.*; public class SerializableClass { //1.简单序列化(ObjectOutputStream和ObjectInputStream用于序列化) public void simpleSerializable(){ Cat c =new Cat(); try { FileOutputStream fs =new FileOutputStream("testSer.ser");//注意:由于要用ObjectOutputStream包装,所以要用字节形式打开文件 ObjectOutputStream os =new ObjectOutputStream(fs); os.writeObject(c);//工作:1.序列化对象;2.将序列化的对象写入文件 os.close(); } catch (FileNotFoundException e) {//在打开文件时,会抛出的异常 e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fis =new FileInputStream("testSer.ser"); ObjectInputStream ois =new ObjectInputStream(fis); c =(Cat)ois.readObject();//抛出异常ClassNotFundException,返回的是Object ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //2.对象图,Java序列化机制会负责保存对象的整个对象图,将所需的一些内容进行深复制. public void ObjectGramm(){ Collar col =new Collar(3); Cat c =new Cat(col,5); System.out.println("before:collar size is "+c.getCollar().getCollarSize()); try { FileOutputStream fs =new FileOutputStream("testSer.ser");//换成.txt文件结果是一样的,但是打开txt后,内容基本都是乱码 ObjectOutputStream os= new ObjectOutputStream(fs); os.writeObject(c); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fis =new FileInputStream("testSer.ser"); ObjectInputStream ois =new ObjectInputStream(fis); c =(Cat)ois.readObject(); ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("after: collar size is " +c.getCollar().getCollarSize()); //outPut is : //before:collar size is 3 //after: collar size is 3 } //3.若不能访问Collar的代码,即无法通过修改的方式实现Serializable接口,可用transient修饰符跳过Collar,然后通过两个私有方法,writeObject()和readObject(),来实现序列化 public void transientUse(){ Collar col =new Collar(3); Dog d =new Dog(col,5); System.out.println("before:collar size is "+d.getCollar().getCollarSize()); try { FileOutputStream fs =new FileOutputStream("testSer.ser"); ObjectOutputStream os= new ObjectOutputStream(fs); os.writeObject(d); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fis =new FileInputStream("testSer.ser"); ObjectInputStream ois =new ObjectInputStream(fis); d =(Dog)ois.readObject(); ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("after: collar size is " +d.getCollar().getCollarSize()); //outPut is : //before:collar size is 3 //after: collar size is 3 } //继承关系对序列化的影响,即:子类可以序列化而父类不可以序列化的情况 //反序列化时,实现Serializable的类,不会调用构造函数,不会初始化;为实现Serializable的类,将初始化。用transient修饰的,若没有上面的那一组方法,则变量为0,对象为null public void superNotSerial(){ Human h =new Human(35, "HaHa"); System.out.println("before: "+h.name+" "+h.weight); try { FileOutputStream fs =new FileOutputStream("testSer.ser"); ObjectOutputStream os= new ObjectOutputStream(fs); os.writeObject(h); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fis =new FileInputStream("testSer.ser"); ObjectInputStream ois =new ObjectInputStream(fis); h =(Human)ois.readObject(); ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("after: "+h.name+" "+h.weight); //outPut is: //before: HaHa 35 //after: HaHa 42 } public static void main(String[] args){ SerializableClass sc =new SerializableClass(); //sc.simpleSerializable(); //sc.ObjectGramm(); //sc.transientUse(); //sc.superNotSerial(); } /** * * .ser文件是Java程序源代码中的一种文件。 * Java 程序包括源代码(.java文件)、由编译器生成的类(.class文件)、 * 由归档工具jar生成的.jar文件、对象状态序列化.ser文件。 * .jar文件是安装的数据文件,.jad文件是安装的信息文件 * */ }
Cat类: package chapter6; import java.io.*; class Cat implements Serializable{ private Collar theCollar; private int catSize; public Cat(Collar collar,int size){ theCollar =collar; catSize =size; } public Cat() { } public Collar getCollar(){ return theCollar; } }
Collar类 package chapter6; import java.io.Serializable; public class Collar implements Serializable{//所有进行深复制的对象,都必须实现Serializable接口 private int collarSize; public Collar(int size) { collarSize =size; } public int getCollarSize(){ return collarSize; } }
Dog类: package chapter6; import java.io.*; public class Dog implements Serializable { transient private Collar theCollar;//transient关键字,表明不用再序列化Collar类了 private int dogSize; public Dog(Collar collar,int size){ theCollar =collar; dogSize =size; } public Collar getCollar(){ return theCollar; } private void writeObject(ObjectOutputStream os){ try { os.defaultWriteObject();//告诉JVM对该对象执行常规的序列化过程 os.writeInt(theCollar.getCollarSize());//将一个额外的Int写入到序列化Dog的流中 } catch (IOException e) { e.printStackTrace(); } } private void readObject(ObjectInputStream is){ try { is.defaultReadObject();//注意读出的时候,要和写入时候的顺序相同。 theCollar =new Collar(is.readInt()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Human类: package chapter6; import java.io.Serializable; public class Human extends Animal implements Serializable { String name; public Human(int w,String n) { weight =w; name =n; } }
Animal类: package chapter6; public class Animal { int weight =42;//初始化为42 }