新手请教:对象串行化

gaoqianjin 2002-12-06 07:14:24
昨天看到了对象串行化,不是很理解!对象流是用来实现数据串行的,串行化使的对象具有持续性,那么持续行是怎么一个概念,是不是说对象在程序执行完之后,被保存仍然存在呢?
对象流,使得数据可以表示成对象的一部分,是说对象流传输的是对象,那么字节流和字符流呢?它们传输的数据就不是对象吗?
...全文
129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlsjisme 2002-12-06
  • 打赏
  • 举报
回复
被序化的对象可以以文件的行式进行保存,或着以流的方式进行传递
fish1980 2002-12-06
  • 打赏
  • 举报
回复
下面列出了被标记为serializable的类:(这些类的子类也都是可序列化的)

java.lang.Character
java.lang.Boolean
java.lang.String
java.lang.StringBuffer
java.lang.Throwable-(包括所有Exception的子类)
java.lang.Number-(包括Integer、Long等)
java.util.Hashtable
java.util.Random
java.util.Vector-(包括Stack)
java.util.Date
java.util.BitSet
java.io.File
java.net.InetAddress
java.rmi.server.RemoteObject

AWT类

基本数据类型数组

对象数组是可序列化的(尽管对象有可能不是可序列化的)
hymarx 2002-12-06
  • 打赏
  • 举报
回复
所谓序列化就是把把对象以一个代码串的形式表示出来,
这样可以保存到磁盘或则通过网络传输,接受方可以在把代码串恢复成对象
具体的串形化方法可以自己写,接收放就可以根据您串形的规则把对象恢复出来
Jacky1206 2002-12-06
  • 打赏
  • 举报
回复
java序列化主要是用于两个方面:
一个是java的RMI(远程方法调用-Remote method invocation),你可以使用别人机器上的对象就像在你自己的机器上使用它一样(通过序列化),另外,在使用javabean时也需要使用序列化,序列化主要就是使用在这两方面。希望对你有帮助,谢谢。
copyright 2002-12-06
  • 打赏
  • 举报
回复
对象序列化的持续性要和永久对象分别开。序列化只能达到有线持续性
在同一个jvm上可以重构对象的内容,达到持续性
虎叔 2002-12-06
  • 打赏
  • 举报
回复
在Java中串行化需要实现串行化的接口:java.io.Serializable
目的是让JVM知道你实现的类的对象可以串行化,比如可以通过网络将你的对象发送到另外的地方。或者可以将对象保存在磁盘上等等!
以下是详细描述:
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.

62,614

社区成员

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

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