我想这将是一个非常基本的问题,但我一直在寻找答案,但我无法弄清楚我的代码出错的地方。所以:我会对一个名为SerializableObject的对象进行序列化,而不是将其读回。在反序列化方法中,我得到一个临时对象,我想将其复制到另一个新的SerializeableObject
,我想在之后使用,但是我无法正确复制它,但是临时对象正确地得到了值,在反序列化。这是我的课程:
SeralizeableObject:
public class SerializableObject implements Serializable, Cloneable{
private Vector<int[]> mixMade;
private Vector<int[]> stepsMade;
private long time;
private int steps;
private int winnerState;
public SerializableObject(Vector<int[]> mixMade, Vector<int[]> stepsMade,
long time, int steps, int winnerState) {
this.mixMade = mixMade;
this.stepsMade = stepsMade;
this.time = time;
this.steps = steps;
this.winnerState = winnerState;
}
@Override
public String toString() {
String str = "";
for(int[] mixEl : mixMade){
str += mixEl[0] + ", " + mixEl[1] + "|";
}
str += " mixes\n";
for(int[] stepEl : stepsMade){
str += stepEl[0] + ", " + stepEl[1] + "|";
}
str += " steps\n";
str += "time: " + time + ", stepsnum: " + steps + ",
winstate: " + winnerState;
return str;
}
@Override
public SerializableObject clone() {
SerializableObject serObj;
Vector<int[]> mixMadeTemp = new Vector<int[]>();
Vector<int[]> stepsMadeTemp = new Vector<int[]>();
for(int i = 0; i < mixMade.size(); ++i){
mixMadeTemp.add(mixMade.get(i));
}
for(int i = 0; i < stepsMade.size(); ++i){
stepsMadeTemp.add(stepsMade.get(i));
}
serObj = new SerializableObject(mixMadeTemp, stepsMadeTemp,
time, steps, winnerstate);
return serObj;
}
}
Serializator:
public class ObjectSerializator {
public ObjectSerializator() {
}
public void toFile(String filepath, SerializableObject serObj){
ObjectOutputStream out;
try{
FileOutputStream fileOut = new FileOutputStream(filepath);
out = new ObjectOutputStream(fileOut);
out.writeObject(serObj);
}catch (IOException ex) {
}
}
public void fromFile(String filepath, SerializableObject serObj){
SerializableObject tempSerObj;
try {
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream in = new ObjectInputStream(fileIn);
tempSerObj = (SerializableObject) in.readObject();
System.out.println(tempSerObj + "TEMP");
serObj = tempSerObj.clone();
in.close();
fileIn.close();
} catch (IOException ex) {
} catch (ClassNotFoundException ex) {
}
}
}
注意:serobj引用在fromFile方法内正确指向了良好的值 一个filechooser类,用户可以从中加载文件:
public class FileChooser extends JFileChooser{
private ObjectSerializator serializator;
public FileChooser() {
serializator = new ObjectSerializator();
}
public void load(SerializableObject serObj){
int retValue = showOpenDialog(null);
serializator.fromFile(getSelectedFile().getAbsolutePath(), serObj);
}}
比我的mainfram我从一个按钮
调用它public void load(){
SerializableObject serObj = new SerializableObject(new Vector<int[]>(), new
Vector<int[]>(), 10, 10, 200);
fileChooser.load(serObj);
System.out.println(serObj + " LAST");
}
这是我的输出:
3, 2|4, 3| mixes
0, 0|0, 1|0, 0| steps
time: 6000, stepsnum: 3, winstate: 0TEMP
-------------------
mixes
steps
time: 10, stepsnum: 10, winstate: 200 LAST
答案 0 :(得分:1)
您反序列化的对象会丢失。
在fromFile
方法中,您有以下一行:
serObj = tempSerObj.clone();
但是serObj
是该方法的一个参数。上面的行更改了 local serObj
变量,而原始对象保持不变。调用方法(load
)仍然保存对旧(原始)对象的引用。
您的fromFile
方法不应接受SerializableObject
作为参数;相反,它应该返回一个。然后FileChooser.load
方法也应该返回它。最后,主框架的load
方法应该类似于:
public void load() {
SerializableObject originalObject = new SerializableObject(new Vector<int[]>(), new Vector<int[]>(), 10, 10, 200);
// Here you can do something with the newly created object, such as save it to a file.
SerializableObject deserializedObject = fileChooser.load();
System.out.println(deserializedObject + " LAST");
}