/** 对象的序列化: **/ import java.io.*; class Animal implements Serializable //实现Serializable 的接口 { private String name; int age; Animal(String name,int age) { this.name = name; this.age = age; } public String toString() { return name+":"+age+""; } } class ObjectOutputStreamDemo { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] agrs)throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\anmial.txt")); //写对象的字节流 oos.writeObject(new Animal("MM",13)); //写一个对象 oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:\\anmial.txt")); Animal ami=(Animal)ois.readObject(); //读一个anmial的对象 sop(ami.toString()); ois.close(); } }
注意的问题: 1. 在类中如果定义了static 变量的对象不能序列化,原因是它们在内存中的区域不一样,定义static 在共享数据区,一个在堆区2.在非静态的变量不想被序列化可以加transient关键字进行修饰即可
3.可以给自定义的类加上static final long serialVersionUID=任意数字的Long 即可,类的属性改变,照样能从硬盘中读出
该程序是模仿网上Tree的形式来写的,用到了注意事项的内容
import java.io.*; class Tree implements Serializable //该接口是个可把对象序列化 { static final long serialVersionUID = 43L; //固定的UID号,防止类的属性变化,UID号也跟着改变 transient private int id; //该ID 数据不会写到硬盘中 private Tree left; private Tree right; private int num; /**************************************************************** * * 产生多少个节点就产生多少个对象 * ***************************************************************/ public Tree(int num) { if((--num)>=0) { left = new Tree(num); //右孩子 right = new Tree(num); } } public void printTree(int num) { /*****************父节点的打印****************************/ if(num==2) { System.out.print("父节点"); } for(int i=0;i<num;i++) { System.out.print("*"); } System.out.println("node"+num); /***************子节点的打印*******************************/ if((--num)>=0) { System.out.print("左孩子节点"); left.printTree(num); System.out.print("右孩子节点"); right.printTree(num); } } } class ObjectOutputStreamTree { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] agrs)throws Exception { //writeObj(); readObj(); } public static void writeObj()throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("c:\\Tree.txt")); oos.writeObject(new Tree(2)); oos.close(); } public static void readObj()throws Exception { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("c:\\Tree.txt")); Tree tr=(Tree)ois.readObject(); //返回一个对象 tr.printTree(2); ois.close(); } }
/** 用Externalizable 的接口来Serailizable 的扩展除了能存对象信息还能存数组,字符串 **/ import java.io.*; class Tree implements Serializable //该接口是个可把对象序列化 { static final long serialVersionUID = 43L; //固定的UID号,防止类的属性变化,UID号也跟着改变 transient private int id; //该ID 数据不会写到硬盘中 private Tree left; private Tree right; private int num; /**************************************************************** * * 产生多少个节点就产生多少个对象 * ***************************************************************/ public Tree(int num) { if((--num)>=0) { left = new Tree(num); //右孩子 right = new Tree(num); } } public void printTree(int num) { /*****************父节点的打印****************************/ if(num==2) { System.out.print("父节点"); } for(int i=0;i<num;i++) { System.out.print("*"); } System.out.println("node"+num); /***************子节点的打印*******************************/ if((--num)>=0) { System.out.print("左孩子节点"); left.printTree(num); System.out.print("右孩子节点"); right.printTree(num); } } } class PointTree implements Externalizable { static final long serialVersionUID = 44L; public PointTree(){} //要构造一个空的无参构造函数(权限必须是public ) private String ss="saw forest"; Tree tr=new Tree(2); /** * 实现Externalizable 的接口必须要复写writeExternal () 和readExternal() 的方法 * *才能有资格调用writeObject() 和readObject() 的方法,调用这两个方法 * *其实就是调用的writeExternal() 和readExternal() * * *可以根据需要定义除了对象别的数据形式。从这里说明了它是Serailizable的扩展 * */ public void writeExternal(ObjectOutput out)throws IOException { out.writeObject(tr); out.writeObject(ss); } public void readExternal(ObjectInput oin)throws IOException,ClassNotFoundException { Tree ss=(Tree)oin.readObject(); ss.printTree(2); String st=(String)oin.readObject(); System.out.println(st); } } class ObjectOutputStreamTree { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] agrs)throws Exception { writeObj(); readObj(); } public static void writeObj()throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("c:\\Tree.txt")); PointTree pt=new PointTree(); oos.writeObject(pt); oos.close(); } public static void readObj()throws IOException,ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("c:\\Tree.txt")); PointTree tr=(PointTree)ois.readObject(); ois.close(); } }
对象的序列化实现Serializable 接口和Externalizable接口
最新推荐文章于 2024-02-23 21:58:15 发布
