[英]java.io.NotSerializableException Error in ObjectInputStream
I am trying to save obj in files but java.io.NotSerializableException error doesnt allow me to do it. 我正在尝试将obj保存在文件中,但是java.io.NotSerializableException错误不允许我这样做。 This is my code:
这是我的代码:
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();
}
}
Class 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;
}
Can someone help me please? 有人能帮助我吗? I really need to put this saving and reading files and what is inside them
我真的需要保存和读取文件以及其中的内容
Error: 错误:
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)
I really dont know what to do 我真的不知道该怎么办
Your class Gerir_jogo
must implements the interface Serializable . 您的类
Gerir_jogo
必须实现接口Serializable 。 Please, take a look at this tutorial . 请看一下本教程 。
Edit 1 编辑1
Your class does not implements it, so, change if for: 您的班级没有实现它,因此,如果需要更改,请执行以下操作:
import java.io.Serializable;
public class Gerir_jogo extends Contentor implements GameContainerContract, Serializable {
Implement Serializable on Gerir_jogo class. 在Gerir_jogo类上实现Serializable。 Change class declaration as:
将类声明更改为:
import java.io.Serializable;
public class Gerir_jogo extends Contentor implements GameContainerContract, Serializable {
To serialize java object means to stream the byte information as non-static states of the object to external file. 序列化Java对象意味着将字节信息作为对象的非静态状态流式传输到外部文件。 Here the class must be implemented by the interface "Serilizable".
在这里,该类必须通过“ Serilizable”接口实现。
Here I have replied with a code, which copies the States as id, name & sal of the class "Employee" to an external file present at the destination "D:\\Object\\States.txt". 在这里,我已回复了一个代码,该代码将“雇员”类的ID,名称和工资的州复制到目标“ D:\\ Object \\ States.txt”中存在的外部文件中。 Here its not readable.
在这里不可读。
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Employee implements Serializable {
int id;
String name;
double sal;
public Employee(int id, String name, double sal) {
super();
this.id = id;
this.name = name;
this.sal = sal;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", sal=" + sal + "]";
}
public static void main(String[] args) throws Exception {
Employee e1 = new Employee(101, "Mr. S.V. Roshan", 45000.0D);
FileOutputStream fout = new FileOutputStream("D:/Object/State.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(e1);
out.flush();
out.close();
fout.flush();
fout.close();
System.out.println("Object serialized successfully!");
}
}
I think, It may help you..! 我认为,这可能会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.