2011-04-24 74 views
2

我有一個Arraylist,我想保存在另一個應用程序中使用的文件中,我已經閱讀了幾個地方,我可以使用此代碼來做到這一點,但它不'將不起作用,它返回一個錯誤它打印後右出3:保存一個ArrayList到一個文件

private void savegame(){ 
    try {System.out.println("1"); 
     FileOutputStream preout = new FileOutputStream(new File("savedgame.ser")); 
     System.out.println("2"); 
     ObjectOutputStream out = new ObjectOutputStream(preout); 
     System.out.println("3"); 
     out.writeObject(kortene); 
     System.out.println("4"); 
     out.close(); 
     System.out.println("5"); 
     preout.close(); 
     System.out.println("6"); 
     output = new FileWriter(new File("saved game settings.txt")); 
     System.out.println("7"); 
     output.write(Indstillinger.BilledeMappe+"\n"+Indstillinger.AntalKort+ 
       "\n"+Indstillinger.AntalSpillere+"\n"+clicks+"\n"+Score); 
     System.out.println("8"); 
     output.close(); 
     System.out.println("game save success"); 
    }catch (Exception e) { 
     System.out.println("game save failed"); 
    } 

和「kortene」就是這樣創造了一個ArrayList的:

public static ArrayList<Kort> kortene = new ArrayList<Kort>(); 

和科爾特是I類製成,但ISN與我認爲的這個問題無關..

我得到的錯誤是:java.io.NotSerializableException:java.awt.image.BufferedImage中

,但我沒有一個BufferedImage,我只是在每個類的正常圖像...

回答

2

我假設您的班級中聲明的字段的類型爲Image,但在運行時分配了一個對象BufferedImage。因此它不能將其寫入輸出文件,因爲BufferedImage不可序列化。