所以我有一个名为 World 的类:
public class World implements Serializable {
private int dx;
private int dy;
private int x;
private int y;
private Path path;
public ImageIcon image;
public ArrayList<Location> locations;
在将对象添加到 ArrayList<Location> locations;
之前,我对它进行了序列化和反序列化,效果很好。
现在当我尝试运行序列化时
try {
FileOutputStream fos = new FileOutputStream(Main.board.worldpath.toString());
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(Main.board.world.image);
oos.writeObject(Main.board.world);
oos.close();
}catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
它抛出 java.io.NotSerializableException: sun.nio.fs.WindowsPath
在这一行oos.writeObject(Main.board.world);
Location 类也是可序列化的:
public class Location implements Serializable {
public int x;
public int y;
public String name;
public String desc;
我不确定为什么它会说它不可序列化,那么序列化 ArrayList 有什么特别之处吗?
最佳答案
问题出在“路径”上。该类不可序列化。 例如,您可以将“路径”保存为字符串并在加载时创建新路径。
为其实现 readObject 和 writeObject 并使路径 transient 。
关于java - 添加到 ArrayList 后出现 NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31635107/