小谈java中的对象序列化

本文介绍了Java对象序列化的两种主要用途:保存对象状态以便恢复及通过网络传输对象。文章详细解释了如何通过实现Serializable接口完成序列化,并给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java对象序列化机制一般来讲有两种用途:
1.需要将对象的状态保存到文件中,而后能够通过读入对象状态来重新构造对象,恢复程序状态
2.使用套接字在网络上传送对象的程序来说,是很有用的。
我们通过让类实现java.io.Serializable 接口可以将类序列化。这个接口是一个制造者(marker)接口。也就是说,对于要实现它的类来说,该接口不需要实现任何方法。它主要用来通知Java虚拟机(JVM),需要将一个对象序列化。
对于这个,有几点我们需要明确:
1.并非所有类都可以序列化,在cmd下,我们输入serialver java.net.socket,可以得到socket是否可序列化的信息,实际上socket是不可序列化的。
2.java有很多基础类已经实现了serializable接口,比如string,vector等。但是比如hashtable就没有实现serializable接口。
将对象读出或者写入流的主要类有两个: ObjectOutputStream与ObjectInputStream 。ObjectOutputStream 提供用来将对象写入输出流的writeObject方法, ObjectInputStream提供从输入流中读出对象的readObject方法。使用这些方法的对象必须已经被序列化的。也就是说,必须已经实现Serializable接口。如果你想writeobject一个hashtable对象,那么,会得到一个异常。
下面举个例子:
None.gifpackagecom.vitamin.Console;
None.gif
importjava.io.*;
None.gif
None.gif
importcom.sun.org.apache.bcel.internal.classfile.JavaClass;
None.gif
None.gif
classStudentimplementsSerializable
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicStringname;
InBlock.gif
publicintage;
InBlock.gif
publicStudent()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
publicStudent(Stringname,intage)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.name=name;
InBlock.gif
this.age=age;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicintgetAge()dot.gif{
InBlock.gif
returnage;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidsetAge(intage)dot.gif{
InBlock.gif
this.age=age;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicStringgetName()dot.gif{
InBlock.gif
returnname;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
publicvoidsetName(Stringname)dot.gif{
InBlock.gif
this.name=name;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
publicclasstest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO自动生成方法存根
InBlock.gif
Studentstu1=newStudent("phinecos",20);
InBlock.gifStudentstu2
=null;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifObjectOutputStreamout
=newObjectOutputStream(newFileOutputStream("c://a.data"));
InBlock.gifout.writeObject(stu1);
InBlock.gifout.flush();
InBlock.gifout.close();
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
ExpandedSubBlockEnd.gif}

InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifObjectInputStreamin
=newObjectInputStream(newFileInputStream("c://a.data"));
InBlock.gifstu2
=(Student)in.readObject();
InBlock.gifin.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exceptione)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstu2
=newStudent();
ExpandedSubBlockEnd.gif}

InBlock.gifSystem.out.println(
"Originaldata:"+stu2.getName()+""+stu2.getAge());
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值