gpt4 book ai didi

ObjectInputStream 中的 java.io.NotSerializedException 错误

转载 作者:行者123 更新时间:2023-12-02 13:01:50 33 4
gpt4 key购买 nike

我正在尝试将 obj 保存在文件中,但 java.io.NotSerializedException 错误不允许我这样做。这是我的代码:

import Estrategia.Gestor_nivel;
import Resources.Exceptions.DuplicateLevelsId;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import pp_epoca_normal.Gerir_jogo;


public class Teste {


public void guardar_ficheiro(Object obt) {
Gerir_jogo teste = (Gerir_jogo) obt;
System.out.println("sad---- " + teste);
Gestor_nivel sad;
try {
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("teste.dat"));
ObjectOutputStream objOut1 = new ObjectOutputStream(new FileOutputStream("teste1.dat"));
objOut.writeObject(teste);
objOut1.writeObject(teste.getObjetos());
sad = (Gestor_nivel) teste.getLevel(0);


objOut.close();
} catch (Exception e) {
e.printStackTrace();
}

}

public void ler_ficheiro() throws FileNotFoundException, IOException, ClassNotFoundException, DuplicateLevelsId {
Gerir_jogo asd;
ObjectInputStream mySecondStream = new ObjectInputStream(new FileInputStream("teste.dat"));
ObjectInputStream mySecondStream1 = new ObjectInputStream(new FileInputStream("teste1.dat"));

asd = (Gerir_jogo) mySecondStream.readObject();
asd.setObjetos((Object[]) mySecondStream1.readObject());

System.out.println("leu--");
mySecondStream.close();
mySecondStream1.close();

}
}

类 gerir jogo

import Container.Contentor;
import Estrategia.Gestor_nivel;
import Resources.Exceptions.DuplicateLevelsId;
import Resources.GameContainerContract;
import Resources.GameLevelContract;

public class Gerir_jogo extends Contentor implements GameContainerContract {
private String nome_jogo;
private boolean mode_jogo_depuracao;

public Gerir_jogo(String nome_jogo, boolean mode_jogo) {
this.nome_jogo = nome_jogo;
this.mode_jogo_depuracao = mode_jogo;
}


@Override
public boolean addNewLevel(GameLevelContract glc) throws DuplicateLevelsId {
Gestor_nivel a;
boolean asd = false;
for (Object objetos : this.getObjetos()) {
a = (Gestor_nivel) objetos;
if(objetos != null)
if (a.getId() == glc.getId()) {
asd = true;
}
}
if (asd == false)
return super.addObject(glc);
return false;
}

@Override
public boolean removeLevel(GameLevelContract glc) {
return super.Encontrar_objeto(super.removeObject(super.findObject(glc)));
}

@Override
public GameLevelContract getLevel(int i) {
return (GameLevelContract) super.getObject(i);
}

@Override
public int getSize() {
return super.getCont();
}

@Override
public boolean getDebugMode() {
return this.mode_jogo_depuracao;
}

@Override
public void setDebugMode(boolean bln) {
this.mode_jogo_depuracao = bln;
}

@Override
public void setName(String string) {
this.nome_jogo = string;
}

@Override
public String getName() {
return this.nome_jogo;
}

有人可以帮我吗?我真的需要把这个保存和读取文件以及里面的内容放在一起

错误:

java.io.NotSerializableException: pp_epoca_normal.Gerir_jogo
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Gravar_ler_ficheiro.Teste.guardar_ficheiro(Teste.java:34)
at pp_epoca_normal.PP_epoca_normal.main(PP_epoca_normal.java:77)

我真的不知道该怎么办

最佳答案

您的类Gerir_jogo必须实现接口(interface)Serializable 。请看一下这个tutorial .

编辑 1

您的类没有实现它,因此,更改 if for:

import java.io.Serializable;
public class Gerir_jogo extends Contentor implements GameContainerContract, Serializable {

关于ObjectInputStream 中的 java.io.NotSerializedException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267540/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com