尝试编写一个程序来创建 java 3d 对象(如球体、颜色立方体...)的二进制文件以供以后使用。但在这样做之前,我必须序列化对象,但我不能这样做,发生错误。
让我们看看程序----
以下类是创建 ColorCube 对象并写入二进制文件的主类
import com.sun.j3d.utils.geometry.ColorCube;
import java.io.*;
public class SerializeDemo {
public static void main(String [] args) {
DoSerialize e = new DoSerialize();
e.cc = new ColorCube(1.4);
try {
FileOutputStream fileOut = new FileOutputStream("e://objectFile.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
try {
out.writeObject(e);
}catch(NotSerializableException i)
{i.printStackTrace();}
out.close();
fileOut.close();
}catch(IOException i)
{i.printStackTrace();}
}
}
这是可序列化的类---
import com.sun.j3d.utils.geometry.ColorCube;
public class DoSerialize implements java.io.Serializable {
public ColorCube cc;
}
错误是----
java.io.NotSerializableException: com.sun.j3d.utils.geometry.ColorCube
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at SerializeDemo.main(SerializeDemo.java:25)
BUILD SUCCESSFUL (total time: 2 seconds)
那么如何克服这个问题呢?请帮忙。谢谢......