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}