请求java.io.Serializable的详细说明

shuki 2003-08-21 08:58:53
我需要的是例子与说明,请不要给我网页。
谢谢
...全文
220 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fast_time 2003-08-29
  • 打赏
  • 举报
回复
它只是告诉虚拟机,当前类是可以序列化的
不需要你做什么,只要你实现这个接口,具体的虚拟机去做
LingFengNB 2003-08-29
  • 打赏
  • 举报
回复
楼上的程序就可以说明串行化的使用,我再加几句,对象的寿命通常随着生成该对象的程序终止而终止,有时候,可能需要将对象的状态保存下来,在需要的时候再将对象恢复,我们把对象的这种记录自己状态的以便将来再生的能力,叫做对象的持续性(PERSISTENCE),对象通过写出描述自己状态的数值来记录自己,这个过程叫对象的串行化Serializable。串行化的主要任务是写出对象实例变量的值。如果变量是另一个对象的引用,则引用的对象也要串行化,这是个递归过程。
shine333 2003-08-29
  • 打赏
  • 举报
回复
sorry XXXInputStream .....
shine333 2003-08-29
  • 打赏
  • 举报
回复
class ObjectYouWannaSave implements Serializable{
int x; int y;
SubObject so = new SubObject();
..........
}

class SubObject implements Serializable{
String s = "abc"; //String is OK, because String is java.io.Serializable
}

public class Save{ // in Save.java
public void main(String[] args) {
ObjectYouWannaSave original = new ObjectYouWannaSave();
original.x = -1; original.y = 100; .....

ObjectOutputStream out = null;
try{
out = new ObjectOutputStream(new FileOutputStream(new File("c:/save.dat")));
out.writeObject(original);
}catch(Exceptione){
}finally{
try{out.close();}catch(Exception e){}
}
}
}

public class Read{ // in Read.java
public void main(String[] args) {
ObjectInputStream in = null;
try{
out = new ObjectInStream(new FileInStream(new File("c:/save.dat")));
ObjectYouWannaSave o = (ObjectYouWannaSave)in.readObject();
System.out.println("x="+o.x);
System.out.println("subobject.string=" + o.so.s);
........
}catch(Exceptione){
}finally{
try{in.close();}catch(Exception e){}
}
}
}

这是个最简单的例子,先运行Save,以后什么时候运行Read都可以(只要c:\save.dat文件还存在就可以了)
cbhyk 2003-08-29
  • 打赏
  • 举报
回复
jdk doc中的说明

public interface Serializable

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable in this case. The error will be detected at runtime.

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:


private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:


ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;

This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.

Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signatute.


ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;

This readResolve method follows the same invocation rules and accessibility rules as writeReplace.

Hodex 2003-08-29
  • 打赏
  • 举报
回复
你要writeObject时,它会判断它是否继承Serializable
shuki 2003-08-29
  • 打赏
  • 举报
回复
up
zez 2003-08-21
  • 打赏
  • 举报
回复
看 effective java program .(java 高效编程).里面解释很详细...
其实java.io.Serializable仅仅是一个空的接口,里面没有任何东西,引入它只是告诉你的程序,这个类可以序列化...

------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧