这是即将被序列化为字节数组的类。
public class DummyClass implements Serializable
{
private static transient final long serialVersionUID = -8483859843874771619L;
public String y;
public DummyClass(String y)
{
this.y = y;
}
public String getY()
{
return this.y;
}
}
这是序列化执行测试
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] bs = null;
try
{
DummyClass dummyClass = new DummyClass("World I Salute you");
out = new ObjectOutputStream(bos);
out.writeObject(dummyClass); // <--- Throws exception here
bs = bos.toByteArray();
}
finally
{
out.close();
bos.close();
}
以及关于堆栈跟踪:
java.io.NotSerializableException: tests.DummyClassTest
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at tests.DummyClassTest.dummyTest(DummyClassTest.java:109)
显而易见的问题是我做错了什么?
干杯。
编辑:
全班
public class DummyClassTest
{
public class DummyClass implements Serializable
{
private static transient final long serialVersionUID = -8483859843874771619L;
public String y;
public DummyClass(String y)
{
this.y = y;
}
public String getY()
{
return this.y;
}
}
@Test
public void dummyTest() throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] bs = null;
try
{
DummyClass dummyClass = new DummyClass("World I Salute you");
out = new ObjectOutputStream(bos);
out.writeObject(dummyClass);
bs = bos.toByteArray();
}
finally
{
out.close();
bos.close();
}
}
}