【发布时间】:2016-04-07 10:30:29
【问题描述】:
我必须保存并加载一个国际象棋游戏。在国际象棋中我有:
public class Chess
{
private Piece[][] pieceArray;
private Board board;
private int moves;
private boolean turn;
...
Set's and get's
}
我必须加载转弯、移动和矩阵。现在我只保存和加载矩阵 (Pieces[][])
现在我有了这些方法可以在另一个类中保存和加载游戏 在这个课程中,我有一个连接到服务器的 FTPClient。
保存游戏:
public boolean saveGame(Chess chess) {
boolean error = false;
try {
File file = new File("game.save");
FileOutputStream fis = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fis);
oos.writeObject(chess.getArray());
oos.close();
// Save that file in the server
FileInputStream fis = new FileInputStream(new File("game.save"));
client.storeFile("game.save", fis);
fis.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
return error;
保存游戏对我没有任何问题,而且很顺利。
现在这是我用来加载游戏的方法,也就是抛出 invalidClassException 的方法。
try {
FileInputStream fis = new FileInputStream(new File("game.save"));
ObjectInputStream ois = new ObjectInputStream(fis);
chess.setArray((Piece[][]) ois.readObject());
chess.paintInBoard();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
每当我尝试读取矩阵时,我都会得到“java.io.InvalidClassException: [LPiece;; 字段的无效描述符”
我已经在 Piece and Chess 中实现了 Serializable 接口。 我已经尝试保存整个 Chess 类,但这样做我还必须在其他 8 个类中实现 Serializable 接口,我正试图避免这种情况。 我必须单独阅读每篇文章吗?
非常感谢。
【问题讨论】:
标签: java serializable objectinputstream