package com.lifeng.seri;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import org.junit.Test;
import com.lifeng.domain.Department;
public class Serial2Obj {
/**
* Object对象与string之间的序列化和反序列化
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void obj2String() throws IOException, ClassNotFoundException{
//Object 序列化成string
Department dept = new Department("1","开发部","开发");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream);
out.writeObject(dept);
String deptString = byteArrayOutputStream.toString("ISO-8859-1");//必须是ISO-8859-1
System.out.println("===>"+deptString);
//String 的反序列化
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(deptString.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Department dept2 = (Department) objectInputStream.readObject();
System.out.println(dept2);
System.out.println(dept2.getDeptID()+"=="+dept2.getDeptName()+"=="+dept2.getDeptDesc());
out.close();
byteArrayOutputStream.close();
objectInputStream.close();
byteArrayInputStream.close();
}
}