初学java,谁能讲讲Serializable接口

xxm_quanMM 2004-11-24 08:57:58
rt!
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijunjiejava 2004-11-24
  • 打赏
  • 举报
回复
Vector对象是一个很灵活的java数据结构,在实际编程中,有时需要我们将一个Vector对象传递给另一个Java程序并保持Vector的数据结构状态,这时,我们可以将需要传递的对象实现java.io.Serializable接口,序列化这个类,由于序列化本身就是允许一个对象在虚拟机之间传送(或者经过一段空间,如在RMI中;或者经过一段时间,比如数据流被保存到一个文件中)。 关于类的序列化请查阅相关资料,本篇不在叙述,下面使用一个简单的程序说明如何把一个Vector对象序列化并放到一个流中(为了测试方便,这里放到一个文件流中,如果换成一个套接字就可以把对象发送给远程请求者)

程序1:把一个Vector对象存储到一个testvector.obj文件里(模拟server socket处理)




import java.io.*;
import java.util.*;

class TestVector implements java.io.Serializable{
private Vector vect=new Vector();

public void add(Object obj){
this.vect.add(obj);
}
public void print(){
System.out.println(this.vect);
}
public static void main(String[] args){
try {
/* 将对象写到一个文件里 */
FileOutputStream objfile = new FileOutputStream("testvector.obj");
/* 创建一个输出流 */
ObjectOutputStream p = new ObjectOutputStream(objfile);
/* 创建一个TestVector对象 */
TestVector tv =new TestVector();
/*给Vector写入几String个对象*/
tv.add("One");
tv.add("Two");
tv.add("Three");
p.writeObject(tv); // 把tv写入流
p.flush();
objfile.close(); // 关闭文件对象
} catch (Exception e) {
e.printStackTrace();
}
}
}



import java.io.*;
import java.util.*;

class ReadVectorObj{
public static void main (String[] args){
try {
/* 打开文件,读取Vector 存放的对象数据 */
FileInputStream objfile = new FileInputStream("testvector.obj");
ObjectInputStream q = new ObjectInputStream(objfile);
/* 获取对象原始数据 */
TestVector myvector = (TestVector)q.readObject();
myvector.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}


bonniewater 2004-11-24
  • 打赏
  • 举报
回复
什么时候能用到?
yulchina 2004-11-24
  • 打赏
  • 举报
回复
关注。。。
wenming168 2004-11-24
  • 打赏
  • 举报
回复
Serializable
是一个标致接口, 表示实现这个接口的类可以保存到磁盘或通过网络传送,其内的对象也必需Serializable
funcreal 2004-11-24
  • 打赏
  • 举报
回复
看文档:
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 if this is not the 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.

kingmaxno1 2004-11-24
  • 打赏
  • 举报
回复
Serializable接口,本身没有任何方法,
主要是为了序列化使用的,就是使输入的流保持原来的状态,不因环境改变而改变
例如:以windows为服务器的对象导入到linux里,和主要用于RMI(远程方法调用)
Serializable使用的时候,比较占用资源,所以如果不是有特殊要求,尽量减少使用
mxlmwl 2004-11-24
  • 打赏
  • 举报
回复
就是序列化接口哦,你想知道什么?
demon007 2004-11-24
  • 打赏
  • 举报
回复
gz

81,114

社区成员

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

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