I have to save and load a chess game. In Chess I have:
public class Chess
{
private Piece[][] pieceArray;
private Board board;
private int moves;
private boolean turn;
...
Set's and get's
}
I would have to load the turn, moves and the matrix. For now Im only saving and loading the matrix (Pieces[][])
Now I have these methods for saving and loading the game in another class In this class I have a FTPClient connected to a server.
Saving the game:
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;
Saving the game gives me no problem and goes smoothly.
And now this is the method I use to load the game, which is the one throwing the 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();
}
Whenever I try to read the matriz I get "java.io.InvalidClassException: [LPiece;; invalid descriptor for field "
I have implemented the Serializable interface in Piece and Chess. I have tried saving the whole Chess class but doing that I would have to implement the Serializable interface in other 8 classes as well and I'm trying to avoid that. Do I have to read each Piece individually?
Thank you a lot.
I tried saving the saves locally and it worked. The problem was that the server I was using corrupted the file every time I uploaded it, giving me that exception. Changing the server did the job.