我有这个例外,我不明白为什么会抛出它,或者我应该如何处理它.
try {
os.writeObject(element);
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
哪里element是一个TransformGroup包含一些其他TransformGroups类的Atom的一个实例:
public class Atom extends Group implements Serializable{
float pozX,pozY;
Group group= new Group();
Color3f blue = new Color3f(new Color(255));
Color3f black = new Color3f(new Color(0));
Sphere AtSph=new Sphere();
public Atom(final float WEIGHT, final int BOUNDS,final float radius,Color3f color)
{
AppSetting ap= new AppSetting(color, black);
AtSph=new Sphere(radius,1,100,ap);
}
}
Run Code Online (Sandbox Code Playgroud)
完整的错误日志:
java.io.NotSerializableException: javax.media.j3d.TransformGroup
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at cls.MolecularBuilder.addAtom(MolecularBuilder.java:511)
at cls.MolecularBuilder$Console.HidrogenItemActionPerformed(MolecularBuilder.java:897)
at …Run Code Online (Sandbox Code Playgroud) 我正在阅读本文以了解有关Java序列化过程的更多信息.在使用时,readObject/writeObject我可以看到两个用例:
writeObject在序列化之前使用它来加密字节码.从安全角度来看,这是件好事.readObject用来执行反序列化后需要立即执行的任何特定代码片段,当然从#1开始,我们甚至可以readObject用来解密序列化对象时被删除的字节代码.在编写自定义readObject/writeObject方法时序列化/反序列化对象时是否还有其他实际情况?或者,如果你能指出我可以看到readObject/writeObject的一些体面和实际用途的任何地方?
我开发了一个大规模的GUI程序,我有很多需要存储locally在命令中的项目数据.
目前,我将所有全局数据结构保存在保存类(项目)中,然后将它们序列化为本地硬盘文件:
public void saveChanges() {
ArrayList<Student> studentList = new ArrayList<>();
ArrayList<String> coursesList = new ArrayList<>();
ArrayList<Requirement> reqList = new ArrayList<>();
ArrayList<Risk> riskList = new ArrayList<>();
for (int x = 0; x < dlm.getSize(); x++) {
studentList.add(dlm.elementAt(x));
}
for (int x = 0; x < dlm2.getSize(); x++) {
coursesList.add(dlm2.elementAt(x));
}
for (int x = 0; x < dlm3.getSize(); x++) {
reqList.add(dlm3.elementAt(x));
}
for (int x = 0; x < riskTable.getRowCount(); x++) {
riskList.add((Risk) riskMap.get(dtm1.getValueAt(x, 0)));
}
project.setStudentAL(studentList);
project.setCoursesAL(coursesList); …Run Code Online (Sandbox Code Playgroud) 这是我使用serialisable接口的部分.当我启动程序时,它将为每个列表创建相同的对象,但具有不同的引用.有没有办法把它们作为一个参考?
private static void quitApplication(){
System.out.println("Stopping the system...\n");
///store all the objects by serializing
try {
FileOutputStream fileOut2 = new FileOutputStream("FlightList.ser");
ObjectOutputStream out2 = new ObjectOutputStream(fileOut2);
out2.writeObject(Flight.getFlights());
out2.close();
fileOut2.close();
}catch(IOException i) {
System.out.println("FlightList.ser ERROR");
}
try {
FileOutputStream fileOut = new FileOutputStream("CityList.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(City.getCityList());
out.close();
fileOut.close();
}catch(IOException i) {
System.out.println("CityList.ser ERROR");
}
try {
FileOutputStream fileOut1 = new FileOutputStream("GrapghL.ser");
ObjectOutputStream out1 = new ObjectOutputStream(fileOut1);
out1.writeObject(Test.flightGraph);
out1.close();
fileOut1.close();
}catch(IOException i) {
System.out.println("GrapghL.ser ERROR");
}
System.out.println("Done...Thank You For …Run Code Online (Sandbox Code Playgroud) 我有一个序列化到文件中的对象列表:
class MyClass implements Serializable {
public String location;
public SomeClass lastUpdatedAt;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我现在有了这个类的新版本,并且我们不再有 SomeClass 了;原因很复杂,但我需要将其序列化回新形式(这是一个示例类,显然不是实际的类):
class MyClass implements Serializable {
public String location;
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我想在反序列化时忽略一个属性。
请注意,我无法更改原始类源文件。我只有新的源文件
但是,当ObjectInputStream尝试在类路径上查找SomeClass时会失败:
try {
FileInputStream fis;
ObjectInputStream is;
File f = getSavedFile();
if (f.exists()) {
fis = new FileInputStream(f);
is = new ObjectInputStream(fis);
ArrayList<MyClass> result = (ArrayList<MyClass>) is.readObject(); //fails
is.close();
fis.close();
}
}
catch (Exception e) {
//ClassNotFound exception because SomeClass is not on the classpath.
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我知道你可以扩展ObjectInputStream并在那里实现一些东西,但我对序列化不太熟悉,希望有人解决同样的问题。