我试图在程序执行期间保留我的java对象的“图片”,以便让用户随后查看该对象的任何状态。
但问题是,虽然它显然序列化了对象的几个状态,但当我反序列化它时,无论我选择的“图片”如何,我都会得到最后一个状态。
我想要序列化的对象之一
public class Board implements IObject{
/**
*
*/
private static final long serialVersionUID = 3964220699069459042L;
private char[][] board;
public static final char X = 'X';
public static final char O = 'O';
public static final char blank = '-';
public Board(int x, int y)
{
board = new char[x][y];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y ; j++) {
board[i][j] = blank;
}
}
}
@Override
public void print()
{
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j <board[i].length; j++ )
{
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
每次我更换主板时,我都会运行以下命令:
roundStatus.addFrame(rf.getPlayer(), rf.getRound(), rf.getStatus());
我的序列化和反序列化函数
public void addFrame(String p, Integer round, IObject board)
{
frames.add(new RoundFrame(p,round,board));
}
String path;
ObjectOutput out;
public void save(String id)
{
path = String.format("%s\\%s\\%s.ser",System.getProperty("user.dir"),LOGPATH,id);
System.out.println(path);
File yourFile = new File(path);
try
{
if(!yourFile.exists())
yourFile.createNewFile();
out = new ObjectOutputStream(new FileOutputStream(path,true));
out.writeObject(getFrames());
out.close();
}
catch(IOException cause)
{
cause.printStackTrace();
}
}
public RoundFrame open(String id, int round)
{
RoundFrame ls=null;
try
{
FileInputStream fileIn = new FileInputStream(String.format("%s\\%s\\%s.ser",System.getProperty("user.dir"),LOGPATH,id));
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println(String.format("%s\\%s\\%s.ser",System.getProperty("user.dir"),LOGPATH,id));
Object orf = in.readObject();
if (orf instanceof ArrayList<?>) {
ArrayList<?> lrf = (ArrayList<?>) orf;
System.out.println(lrf.size());
for(Object obj : lrf)
{
RoundFrame rf = (RoundFrame) obj;
System.out.println(rf.getRound());
if(rf instanceof RoundFrame)
{
rf.getStatus().print();
}
}
for(Object obj : lrf)
{
RoundFrame rf = (RoundFrame) obj;
System.out.println(rf.getRound());
if(rf.getRound() == round && rf instanceof RoundFrame)
{
ls = rf;
break;
}
}
}
in.close();
fileIn.close();
}catch (FileNotFoundException e) {
System.err.println("File not found: " + e);
}catch (EOFException e) {
System.err.println("End of stream: " + e);
}catch (IOException e) {
System.err.println("Read error: " + e);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ls;
}
还有我的 Frame 类
public class RoundFrame implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 3565826725666895011L;
private String player;
private Integer round;
private IObject status;
public RoundFrame()
{
}
public RoundFrame(String p, Integer round, IObject board)
{
setPlayer(p);
setRound(round);
setStatus(board);
}
/*gets and sets*/
}
回合和玩家都被很好地序列化,即使 IObject 也被序列化,我总是得到同一帧的 N 个副本。
是否可以保存同一个对象的多个状态?
最佳答案
每次你打电话
roundStatus.addFrame(rf.getPlayer(), rf.getRound(), rf.getStatus());
您正在传递对棋盘对象的引用。如果您编辑该板并再次调用它,则新框架和最后一个框架将指向同一对象,并且将具有相同的内部值。
我建议使用不可变的对象来代表董事会,这样你就可以轻松实现你想要做的事情并避免这样的错误。
关于java - 如何序列化对象的多个帧而不获取当前的反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34243335/