我现在正在墙上撞到我的头。
这是交易:我创建了一个BlockList类,我创建了一个块列表。这个类是Serialized,Block类也是(也是我在Block类中使用的RGB类)。
因此,当我尝试使用ObjectOutputStream将列表保存在二进制文件中时,一切正常。
但是当我尝试读取文件时,我得到了这个NotSerializable异常。我的意思是,列表中可能包含的所有类都是Serializable。为什么我甚至会收到此错误?
public void saveToHardDrive(){
try{
FileOutputStream fos = new FileOutputStream("blocks.bk", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
}catch(IOException e){
System.out.println("ERREUR : erreur lors de la sauvegarde.");
}
}
public void loadFromHardDrive(){
try {
FileInputStream fis = new FileInputStream("blocks.bk");
ObjectInputStream ois = new ObjectInputStream(fis);
list = (ArrayList<Block>) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
如果有人能找到,我欠你一生。
PS:甚至更奇怪:当我尝试加载文件时,错误也提到了保存功能......即使我评论了所有功能。
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: vector.Block
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(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 vector.BlockList.loadFromHardDrive(BlockList.java:60)
at vector.main.file(main.java:184)
at vector.Controls.Controls(Controls.java:57)
at vector.main.gameLoop(main.java:111)
at vector.main.main(main.java:171)
Caused by: java.io.NotSerializableException: vector.Block
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(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 vector.BlockList.saveToHardDrive(BlockList.java:51)
at vector.main.file(main.java:189)
at vector.Controls.Controls(Controls.java:57)
at vector.main.gameLoop(main.java:113)
at vector.main.main(main.java:173)
编辑:这是Block类:
public class Block implements Serializable{
private float x;
private float y;
private float height;
private float length;
private RGB color;
public Block(float x, float y){
this.x = x;
this.y = y;
length = 100;
height = 100;
color = new RGB(1, 1, 1);
}
public Block(Player p){
setX(p.getX());
setY(p.getY());
setHeight(p.getHeight());
setLength(p.getLength());
setColor(new RGB(1, 1, 1));
}
public void render() {
Quad.renderQuad(this);
}
和RGB以防万一:
public class RGB implements Serializable{
float r, g, b;
public RGB(float R, float G, float B){
r=R;g=G;b=B;
}
public String toString(){
return (r*250) + ", " + (g*250) + ", " + (b*250);
}
}
答案 0 :(得分:3)
您在创建vector.Block Serializable之前创建了该文件。你必须重新创建它。
答案 1 :(得分:0)
所有应序列化的类必须实现Serializable
- 接口。