java中集合的序列化_Java中对象集合的序列化

java的IO中提供了一个 ObjectOutputStream对象,其中的方法writeObject()文档如下:

/**

* Write the specified object to the ObjectOutputStream. The class of the

* object, the signature of the class, and the values of the non-transient

* and non-static fields of the class and all of its supertypes are

* written. Default serialization for a class can be overridden using the

* writeObject and the readObject methods. Objects referenced by this

* object are written transitively so that a complete equivalent graph of

* objects can be reconstructed by an ObjectInputStream.

*

*

Exceptions are thrown for problems with the OutputStream and for

* classes that should not be serialized. All exceptions are fatal to the

* OutputStream, which is left in an indeterminate state, and it is up to

* the caller to ignore or recover the stream state.

*

* @throwsInvalidClassException Something is wrong with a class used by

*serialization.

* @throwsNotSerializableException Some object to be serialized does not

*implement the java.io.Serializable interface.

* @throwsIOException Any exception thrown by the underlying

* OutputStream.

*/

public final void writeObject(Object obj) throws IOException {

}

简要的翻译就是“将指定的对象写到输出流”。其实这个方法的文档有些误导的意思,下面这个例子我们会发现,这个方法甚至可以直接序列化集合,如List,Array等。

首先建立2个对象,

package serialization;

import java.io.Serializable;

class Employee implements Serializable

{

/**

*

*/

private static final long serialVersionUID = -316102412618444933L;

public Employee(String n, double s)

{

name = n;

salary = s;

}

/**

*加薪水

*/

public void raiseSalary(double byPercent)

{

double raise = salary * byPercent / 100;

salary += raise;

}

public String toString()

{

return getClass().getName()

+ "[name = "+ name

+ ",salary = "+ salary

+ "]";

}

private String name;

private double salary;

}

package serialization;

class Manager extends Employee

{

public Manager(String n, double s)

{

super(n, s);

secretary = null;

}

/**

*设置秘书

*/

public void setSecretary(Employee s)

{

secretary = s;

}

public String toString()

{

return super.toString()

+ "[secretary = "+ secretary

+ "]";

}

//secretary代表秘书

private Employee secretary;

}

然后直接序列化集合到文件,并通过读取文件,修改数据来验证序列化是成功的:

package serialization;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;

/**

* 对象的序列化

*

*/

public class Test

{

public static void main(String[] args)

{

Employee harry = new Employee("Harry Hacker", 50000);

Manager manager1 = new Manager("Tony Tester", 80000);

manager1.setSecretary(harry);

List staff = new ArrayList(2);

staff.add(harry);

staff.add(manager1);

//数组也可以

// Employee[] staff = new Employee[2];

//

// staff[0] = harry;

// staff[1] = manager1;

try

{

ObjectOutputStream out = new ObjectOutputStream(

new FileOutputStream("employee.dat"));

out.writeObject(staff); //!!!!!!这个方法可以写集合、数组

out.close();

ObjectInputStream in = new ObjectInputStream(

new FileInputStream("employee.dat"));

List newStaff = (List)in.readObject();

in.close();

/**

*通过harry对象来加薪

*将在secretary上反映出来

*/

newStaff.get(0).raiseSalary(10);

for (int i = 0; i < newStaff.size(); i++)

System.out.println(newStaff.get(i));

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值