1、对象串行化的概念?在书上。
2、对象串行化用处:
(1)在网络中传送对象。
(2)在程序的运行期间将对象保存于文件,或者稍后在相同的应用程序中写入然后读取对象。
3、定制串行化:包括“部分定制”与“完全定制串行化”。
缺省的串行化机制,对象串行化首先写入类数据和类字段的信息,然后按照名称的上升排列顺序写入其数值。如果想自己明确地控制这些数值的写入顺序和写入种类,必须定义自己的读取数据流的方式。就是在类的定义中重写writeObject()和readObject()方法。
4、公司项目中要对Task 进行归档(Archive),也就是将Task对象以流的形式写入存储中心(MMS),并且要提供恢复(retrieve)对象的接口,由于Task包含一个附件列表(Attachment List)属性,需求规定要将其附件一并进行归档。因此简单的缺省串行化已经不能满足需求,必须进行自定义串行化来解决该问题。
5、以下是一个简单的“部分定制串行化”示例,并无太多技术含量,在此记录是便于以后复习温故。在这个示例中,还是以老高的Person和Hobby为例,Person对象包含一个File列表属性,在对Person进行串行化的时候将File实体一并进行串行化,当然为了提高读写速度,采用到了缓冲流来进行包装以提高文件的读写效率。
代码:
package cn.hunnu.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Person class
* @author Administrator
*
*/
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String sex;
private String address;
private List<Hobby> hobbies;
private List<File> attachments;
public Person(){}
public Person(String name,int age,String sex,String address){
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(List<Hobby> hobbies) {
this.hobbies = hobbies;
}
public List<File> getAttachments() {
return attachments;
}
public void setAttachments(List<File> attachments) {
this.attachments = attachments;
}
public void info(){
System.out.println("My name is "+name+".");
System.out.println("I am years "+age+" old.");
System.out.println("sex is "+sex+".");
System.out.println("I am live in "+address);
if(hobbies!=null&&hobbies.size()>0){
System.out.println("My hobbies are:");
for(Hobby hobby:hobbies){
hobby.sayHobbby();
}
}
}
//定制串行化的writeObject(),readObject()方法
private void writeObject(ObjectOutputStream out) throws IOException{
// out.writeUTF(name);
// out.writeInt(age);
// out.writeUTF(sex);
// out.writeUTF(address);
// out.writeObject(hobbies);
// out.writeObject(attachments);
out.defaultWriteObject();
if(attachments!=null&&attachments.size()>0){
List<Long> sizeList = new ArrayList<Long>();
for(File file:attachments){
sizeList.add(file.length());
}
out.writeObject(sizeList);
BufferedOutputStream bout = new BufferedOutputStream(out);
for(File file :attachments){
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int len = 0;
byte[] temp;
byte[] per = new byte[1024];//1 KB
while((len=bis.read(per))>0){
if(len<per.length){
temp = new byte[len];
System.arraycopy(per,0,temp,0,len);
}else{
temp = per;
}
//out.write(temp);
bout.write(temp);
}
//fis.close();
bis.close();
bout.flush();
}
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
// name = in.readUTF();
// age = in.readInt();
// sex = in.readUTF();
// address = in.readUTF();
// hobbies = (List<Hobby>)in.readObject();
// attachments = (List<File>)in.readObject();
in.defaultReadObject();
if(attachments!=null&&attachments.size()>0){
List<Long> sizeList = (List<Long>)in.readObject();
for(Long ik :sizeList){
System.out.println("size:"+ik);
}
BufferedInputStream bis = new BufferedInputStream(in);
for(int i = 0;i<sizeList.size();i++){
File file = attachments.get(i);
String path = "D:/temp/"+file.getName();
File destFile = new File(path);
if(!destFile.exists()){
destFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream bout = new BufferedOutputStream(fos);
byte[] per = new byte[1024];//1KB
long count = sizeList.get(i).longValue()/per.length;
long residueBytes = sizeList.get(i).longValue()%per.length;
for(int j = 0;j<count;j++)
{
//in.read(buff);
//fos.write(buff);
bis.read(per);
bout.write(per);
}
if(residueBytes>0){
byte[] left = new byte[(int)residueBytes];
//in.read(left);
//fos.write(left);
bis.read(left);
bout.write(left);
}
bout.flush();
bout.close();
}
}
}
}
package cn.hunnu.test;
import java.io.Serializable;
/**
* Hobby class
* @author Administrator
*
*/
public class Hobby implements Serializable{
private static final long serialVersionUID = 1L;
private String name ;
public Hobby(){}
public Hobby(String name){
this.name = name;
}
public void sayHobbby(){
System.out.println("Hobby is "+name);
}
}
测试入口:
package cn.hunnu.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* test entrance
* @author Administrator
*
*/
public class Main {
public static void main(String args[]){
//construct object
Person me = new Person("Bo Xiao",23,"male","布心一村");
List<Hobby> hobbies = new ArrayList<Hobby>();
Hobby h1 = new Hobby("看书");
Hobby h2 = new Hobby("乒乓球");
Hobby h3 = new Hobby("电影");
Hobby h4 = new Hobby("音乐");
Hobby h5 = new Hobby("山水");
hobbies.add(h1);
hobbies.add(h2);
hobbies.add(h3);
hobbies.add(h4);
hobbies.add(h5);
me.setHobbies(hobbies);
List<File> files = new ArrayList<File>();
File f1 = new File("D:\\Documents\\Document for CIM\\Task_Management_Overview_V02.ppt");//1168KB
File f2 = new File("D:\\Documents\\Document for CIM\\2010 Direct Client Satisfaction Surveys.zip");//405KB
File f3 = new File("D:\\Documents\\Document for CIM\\CIM_BottomPanel.pdf");//895KB
files.add(f1);
files.add(f2);
files.add(f3);
me.setAttachments(files);
String path = "D:/object.txt";
//write to file
try{
ObjectOutputStream fout = new ObjectOutputStream(new FileOutputStream(path));
fout.writeObject(me);
fout.flush();
fout.close();
}catch(Exception e){
e.printStackTrace();
}
//read from file
try{
ObjectInputStream fin = new ObjectInputStream(new FileInputStream(path));
Person wo = (Person) fin.readObject();
fin.close();
wo.info();
}catch(Exception e){
e.printStackTrace();
}
}
}