将对象转换为字节数组会引发NotSerializableException

时间:2013-11-27 05:45:28

标签: java serialization

我的代码抛出java.io.NotSerializableException。为什么?谢谢!

private void test3() {
   Element3 element3=new Element3();
   ObjectToBytes3(element3);
}

private class Element3 implements Serializable{
  int code=0;
}

//-----------------------------------
private void ObjectToBytes3(Element3 elem){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(elem);
        byte[] myBytes = bos.toByteArray();
    }
    catch (Exception e) {myPrint("exception",e.toString());}
    finally {
        try{
            out.flush();
            out.close();
            bos.close();
        }
        catch (Exception e) {}
    }
}

输出和堆栈如下: 2674,25 ObjToBytes: 2674,32异常:java.io.NotSerializableException:java.util.concurrent.ThreadPoolExecutor stack = java.io.NotSerializableException:java.util.concurrent.ThreadPoolExecutor     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)     在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)


这很有趣,但下面的代码可以正常使用

================================
class My implements Serializable{
    byte a=1;
    int b=88;
    String s="ggg";
    private My(byte a1, int b1,String s1) {
        a=a1;
        b=b1;
        s=s1;
    }
}

private void test() {
    My my=new My((byte)5,10,"jjj");
    ObjectToBytesTest(my);
}

//-----------------------------------
private void ObjectToBytesTest(My my){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(element);
        byte[] myBytes = bos.toByteArray();
    }
    catch (Exception e) {print("exception",e.toString());}
    finally {
        try{
            out.flush();
            out.close();
            bos.close();
        }
        catch (Exception e) {}
    }
}

1 个答案:

答案 0 :(得分:2)

因为Element3实例elem具有对outter对象的隐式引用。

序列化Element3时使用Serializable接口的默认动作,它将序列化outter对象,而outter对象没有实现Serializable接口,因此会抛出java.io.NotSerializableException。

异常将引用未实现Serializable接口的outter对象类。

在我的代码中,它是:java.io.NotSerializableException: serializable.ObjectToBytesTest

要解决这个问题,你应该将Element3类移出outter类,如下所示:

    public class ObjectToBytesTest {
      private void ObjectToBytes3() { ... }
    }

    class Element3 implements Serializable {
        int code=0;
    }

或者就像您的解决方案(我的类实现Serializable接口),但我认为不建议使用此解决方案。