2017-05-30 36 views
0

我想保存文件中的obj,但java.io.NotSerializableException錯誤不允許我這樣做。 這是我的代碼:在ObjectInputStream中的java.io.NotSerializableException錯誤

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) 

我真的不知道該怎麼辦

+0

請提供Gerir_jogo類的錯誤stacktrace和源代碼 –

+0

我已經把類 –

+0

MiguelSouza,檢查我的答案,你的類沒有實現Serializable接口,改變它來實現它。一個類實現多個接口沒有問題,這是允許的。 – BrunoDM

回答

1

你的類Gerir_jogo必須實現接口Serializable。請看看tutorial

編輯1

你的類沒有實現,所以,改變如果:

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

但該類已經實現了一個接口 –

+1

一個類可以實現兩個接口! – BrunoDM

0

落實Gerir_jogo類Serializable接口。 更改類聲明爲:

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

要序列的Java對象是指以流的字節的信息作爲對象,以外部文件的非靜態的狀態。在這裏,這個類必須通過「Serilizable」接口來實現。

在這裏,我回復了一個代碼,該代碼將狀態複製爲ID爲「Employee」類的& sal到存在於目標「D:\ Object \ States.txt」的外部文件。這裏它不可讀。

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!"); 


    } 

} 

我想,它可能會幫助你..!

相關問題