私は非常に単純なチェスゲームをGUIとロジックで別々に書いています。「ゲームを保存」ボタンがFileDialogを保存し、ファイル名はゲームロジッククラスのsavegameメソッドに渡されます。そこではFileOutputStreamでファイルを作成し、ObjectOutputStreamでこのファイルに書き込もうとします。しかし、IOExceptionが発生し、私は理由を知りません。Java GUIの「ゲームを保存」ボタンをクリックしてチェスのゲームロジックを連続してシリアル化しない
ここには、ゲームクラスがどのようにチェスゲームプログラムのロジック部分であるかのスニペットがあります。
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
異論からスタックトレースを提供してください。 –