如何在Java中锁定序列化? [英] How to lock serialization in Java?

查看:131
本文介绍了如何在Java中锁定序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Java序列化:我有一个练习要做,我需要锁定任何类的序列化,假设在我尝试序列化该类时抛出异常。

I am just starting with Java serialization: I have one exercise to do and I need to lock serialization on any class, it is suppose to throw an exception when I attempt to serialize that class.

有谁知道怎么做?

推荐答案

如果添加<$ c $的实现c> writeObject 会抛出异常,序列化将被中止,例如

If you add an implementation of writeObject which throws an exception, serialization will be aborted, e.g.

  private void writeObject(ObjectOutputStream stream) throws IOException {
    throw new RuntimeException("Don't want to serialize today");
  }

参见 http:/ /java.sun.com/developer/technicalArticles/ALT/serialization/ 有关覆盖默认序列化行为的详细介绍。

See http://java.sun.com/developer/technicalArticles/ALT/serialization/ for a good introduction to overriding the default serialization behaviour.

这篇关于如何在Java中锁定序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆