对象的序列化实现Serializable 接口和Externalizable接口

本文详细介绍了Java中对象序列化的实现方法,包括使用Serializable接口序列化对象实例、排除静态变量和使用transient关键字忽略某些字段的过程。此外,还探讨了通过实现Externalizable接口来扩展序列化功能的方法。
/**
对象的序列化:
**/
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();
	}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值