我正在用单独的gui和逻辑编写相当简单的国际象棋游戏。“保存游戏”按钮开始保存FileDialog,并从那里将用户选择的目录和文件名传递给游戏逻辑类中的savegame方法。在那里我通过FileOutputStream创建文件,然后尝试使用ObjectOutputStream写入此文件。但是引发了IOException,我不知道为什么。
以下是Game类看起来的片段,这是国际象棋游戏程序的逻辑部分。
public class Game implements Runnable, Serializable {
private final Board board;
private final transient MoveEvaluate moveEvaluator;
private final Logger log = Logger.getLogger("game");
private transient BoardGui gameGui;
private boolean whiteOnMove;
private boolean gameOver = false;
private boolean customBoardSetup = true;
private boolean setupIsDone = false;
public Game() {
log.log(Level.SEVERE, "game created");
this.board = new Board();
this.whiteOnMove = true;
this.moveEvaluator = new MoveEvaluate(this);
}
这是保存游戏方法,在游戏类中。这个方法是从gui动作点击监听器调用的。
public void saveGame(String directoryName, String fileName) {
try {
FileOutputStream fileOut = new FileOutputStream(directoryName+fileName+".ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
catch (IOException e) {
e.printStackTrace();
log.log(Level.SEVERE, "exception while serializing : IOException");
}
}
我有这种方法out.writeObject(this);
,我认为这可能是问题所在。 Game类实现了Runnable。
问题是问题出在哪里。当gui和逻辑线程运行时,如何处理这样的逻辑序列化。 谢谢
以下是此IOException的堆栈跟踪
SEVERE: File crated: C:\Users\Dagmar Kole4k85ov8\Desktop\l.ser
Kv? 29, 2016 10:32:30 DOP. com.mycompany.chess.Game saveGame
SEVERE: fileOut: C:\Users\Dagmar Kole4k85ov8\Desktop\l.ser
Kv? 29, 2016 10:32:30 DOP. com.mycompany.chess.Game saveGame
SEVERE: ObjectOutputStream made
java.io.NotSerializableException: java.util.logging.Logger
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.mycompany.chess.Game.saveGame(Game.java:170)
at com.mycompany.chess.GameGui.saveGame(GameGui.java:54)
at com.mycompany.chess.BoardMenuGui.actionPerformed(BoardMenuGui.java:46)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Kv? 29, 2016 10:32:30 DOP. com.mycompany.chess.Game saveGame
SEVERE: exception while serializing : IOException
答案 0 :(得分:0)
我自己已经发现了这个错误。问题是Logger未在所有被序列化的类中设置为transient
。在向所有人添加瞬态后,游戏成功保存。