首页 > 代码库 > java 的序列化

java 的序列化

(1) 首先是java自己内部实现的对象序列化机制 其实就是ObjectInputStream 和 ObjectOutputStream

首先实现一个实体对象  记住必须实现Serializable 接口

package com.rpc;import java.io.Serializable;public class Student implements Serializable{		private static final long serialVersionUID = 5915058853403173212L;		private String id;	private String name;	private String sex;	private int age;		public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getSex() {		return sex;	}	public void setSex(String sex) {		this.sex = sex;	}	public int getAge() {		return age;	}	public void setAge(int age) {		this.age = age;	}		}

 然后是将对象以文件的形式保存

package com.rpc;import java.io.File;import java.io.FileOutputStream;import java.io.ObjectOutputStream;public class ObjectChuanshu {	public static void main(String[] args) {		Student student = new Student();		student.setAge(23);		student.setId("123");		student.setName("张三");		student.setSex("男");				FileOutputStream fout = null;		ObjectOutputStream oos = null;		try{			fout = new FileOutputStream(new File("d:/1.object"));			oos = new ObjectOutputStream(fout);			oos.writeObject(student);			oos.flush();			fout.flush();		}catch(Exception e){			System.out.println(e.getMessage());			e.printStackTrace();		}finally{			try{				oos.close();				fout.close();			}catch(Exception e){}					}	}}

 然后是从文件中读出对象

package com.rpc;import java.io.File;import java.io.FileInputStream;import java.io.ObjectInputStream;public class ReadObject {	public static void main(String[] args) {				FileInputStream fin = null;		ObjectInputStream ois = null;				try{			fin = new FileInputStream(new File("d:/1.object"));			ois = new ObjectInputStream(fin);			Student student = (Student)ois.readObject();			System.out.println(student.getName());		}catch(Exception e){			System.out.println(e.getMessage());			e.printStackTrace();		}finally{			try{				fin.close();				ois.close();			}catch(Exception e){}		}			}}

 输出结果

张三

 Hessian 的对象 序列化  需要添加 hessian.jar

package com.rpc;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import com.caucho.hessian.io.Hessian2Input;import com.caucho.hessian.io.Hessian2Output;public class HessianObject {	private String filePath;		public HessianObject(String filePath){		this.filePath = filePath;	}	/**	 * 将对象保存到指定的文件中	 * @param filePath	 * @throws Exception	 */	public byte[] writeObject(Student student) throws Exception{				Hessian2Output hout = null;		ByteArrayOutputStream baos = null;		try{			baos = new ByteArrayOutputStream();			hout = new Hessian2Output(baos);			hout.writeObject(student);			hout.flush();		}catch(Exception e){			throw e;		}finally{		}		return baos.toByteArray();	}		/**	 * 读取文件中的对象	 * @param filePath	 * @throws Exception	 */	public Student readObject(byte[] buffer)throws Exception{				Hessian2Input hin = null;		Student student = null;		ByteArrayInputStream bais = null;		try{            bais = new ByteArrayInputStream(buffer);			hin = new Hessian2Input(bais);			student =  (Student)hin.readObject();		}catch(Exception e){			throw e;		}finally{		}		return student;	}		public static void main(String[] args) {				HessianObject hessianObject = new HessianObject("d:/2.object");		Student student = new Student();		student.setAge(18);		student.setId("12345");		student.setName("李四");		student.setSex("女");		try{			byte[] buffer = hessianObject.writeObject(student);				Student s = hessianObject.readObject(buffer);				System.out.println(s.getName());		}catch(Exception e){			System.out.println(e.getMessage());			e.printStackTrace();		}	}}

 输出结果

李四

 

java 的序列化