gpt4 book ai didi

java - 如何使用反序列化使我的对象恢复生机?

转载 作者:行者123 更新时间:2023-12-01 18:39:05 30 4
gpt4 key购买 nike

我将类对象序列化到文件中,如下所示:

public class GreenhouseControls extends Controller implements Serializable {

private static final long serialVersionUID = -7079669536704670179L;
private long numOfRings;
private long rings;
private Event event;
private Scanner s;
private ArrayList<String> array;
private ArrayList<String> array2;
private ArrayList<String> array3;
public boolean windowok;
public boolean poweron;
public boolean fans;
public boolean light;
public boolean water;
public String thermostat;
private String eventsFile;
public int errorcode;
private String dumpFile;

......

public void saveState() {
try {
// Serialize data object to a file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("dump.out"));
out.writeObject(GreenhouseControls.this);
out.close();
} catch (IOException e) { }
}
.....
}

文件存在,如下所示:

Package Explorer Image .

现在我想反序列化该对象。这是我到目前为止所拥有的:

public class Restore extends Event {

public Restore(long delayTime, String dumpFile){
super(delayTime);
eventsFile = dumpFile;
}
@Override
public void action() throws ControllerException {
try {
FileInputStream fis = new FileInputStream(eventsFile);
ObjectInputStream ois = new ObjectInputStream(fis);
GreenhouseControls gc = (GreenhouseControls) ois.readObject(); // Error occurs here

ois.close();
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public String toString() {
return "Restoring system";
}
}

上面的 Restore 类是否接近我反序列化对象所需的操作?当我运行程序时,我收到 WriteAbortedException 和 NotSerializedException。

以下是异常(exception)情况:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.util.Scanner
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at greenhouse.GreenhouseControls$Restore.action(GreenhouseControls.java:374)
at other.Controller.run(Controller.java:39)
at greenhouse.GreenhouseControls.main(GreenhouseControls.java:530)
Caused by: java.io.NotSerializableException: java.util.Scanner
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at greenhouse.GreenhouseControls.saveState(GreenhouseControls.java:487)
at other.Controller.run(Controller.java:43)
at greenhouse.GreenhouseControls.main(GreenhouseControls.java:524)

这是我的事件类:

import java.io.Serializable;

import greenhouse.GreenhouseControls.ControllerException;

public abstract class Event implements Serializable{
private long eventTime;
protected final long delayTime;


public Event(long delayTime) {
this.delayTime = delayTime;
start();
}

public void start() { // Allows restarting
eventTime = System.currentTimeMillis() + delayTime;
}

public boolean ready() {
return System.currentTimeMillis() >= eventTime;
}

public abstract void action() throws ControllerException;
}

最佳答案

您无法序列化扫描仪。或任何类型的输入或输出流、读取器、写入器、套接字、连接......使扫描器 transient 。最好在任何使用它的方法中将其作为局部变量。您不太可能在整个类(class)中使用相同的扫描仪,如果您是这样,我建议您做错了什么。

关于java - 如何使用反序列化使我的对象恢复生机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673547/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com