JAVA序列化的两种方式计算机等级考试

文章作者 100test 发表时间 2010:01:01 12:16:32
来源 100Test.Com百考试题网


  大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输.
  Serializable 在我们实现这个接口的时候,我们可以使用4个私有方法来控制序列化的过程:
  我们来看一个例子:
  public class FooImpl implements java.io.Serializable{
  private String message.
  public String getFoo() {
  return message.
  }
  public void setMessage(String message) {
  this.message = message.
  }
  private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  System.out.println("writeObject invoked").
  out.writeObject(this.message == null ? "hohohahaha" : this.message).
  }
  private void readObject(java.io.ObjectInputStream in) throws IOException,
  ClassNotFoundException {
  System.out.println("readObject invoked").
  this.message = (String) in.readObject().
  }
  private Object writeReplace() throws ObjectStreamException {
  System.out.println("writeReplace invoked").
  return this.
  }
  private Object readResolve() throws ObjectStreamException {
  System.out.println("readResolve invoked").
  return this.
  }
  public Object serialize() throws IOException, ClassNotFoundException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream().
  ObjectOutputStream oos = new ObjectOutputStream(baos).
  oos.writeObject(this).
  ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()).
  ObjectInputStream ois = new ObjectInputStream(bais).
  return ois.readObject().
  }
  public static void main(String[] args) throws IOException,
  ClassNotFoundException {
  FooImpl fooimpl = new FooImpl().
  fooimpl.serialize().
  }
  }
  public class FooImpl implements java.io.Serializable{
  private String message.
  public String getFoo() {
  return message.
  }
  public void setMessage(String message) {
  this.message = message.
  }
  private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  System.out.println("writeObject invoked").
  out.writeObject(this.message == null ? "hohohahaha" : this.message).
  }
  private void readObject(java.io.ObjectInputStream in) throws IOException,
  ClassNotFoundException {
  System.out.println("readObject invoked").
  this.message = (String) in.readObject().
  }
  private Object writeReplace() throws ObjectStreamException {
  System.out.println("writeReplace invoked").
  return this.
  }
  private Object readResolve() throws ObjectStreamException {
  System.out.println("readResolve invoked").
  return this.
  }
  public Object serialize() throws IOException, ClassNotFoundException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream().
  ObjectOutputStream oos = new ObjectOutputStream(baos).
  oos.writeObject(this).
  ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()).
  ObjectInputStream ois = new ObjectInputStream(bais).
  return ois.readObject().
  }

相关文章


J2ME文件上传实例(FileConnector链接文件系统,非RMS)计算机等级考试
使用Collator进行本地化语言的排序计算机等级考试
在Android(OPhone)模拟器中加载和使用SDCard卡计算机等级考试
JAVA性能优化—IBMJDKJVM参数设置计算机等级考试
JAVA序列化的两种方式计算机等级考试
关于jar包中MANIFEST.MF的问题计算机等级考试
实现Comparator接口来进行字符串逆向排序计算机等级考试
建立自己的IM与GTALK通信计算机等级考试
利用delegate调试Ajax应用计算机等级考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛