简体   繁体   English

带有 java.awt.geom.Area 的 NotSerializableException

[英]NotSerializableException with java.awt.geom.Area

I'm creating a game where all locations of 'blocks' are stored in the variable block_area - an object of class Area .我正在创建一个游戏,其中“块”的所有位置都存储在变量block_area - 类Area的对象。 My game has been running correctly for a week now, and I've decided to implement a save and load feature where I save block_area to a file Drifter , with this as my code:我的游戏已经正常运行了一个星期,我决定实现一个保存和加载功能,我将block_area保存到一个文件Drifter ,并将其作为我的代码:

Area block_area; // Later initialized

void saveArea()
{
    try
    {
        FileOutputStream fos = new FileOutputStream(savefile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(block_area);
        oos.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

void loadArea()
{
    try
    {
        FileInputStream fis = new FileInputStream(savefile);
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        block_area  = (Area)ois.readObject();
        
        ois.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

However, this is my very first time writing and reading an OBJECT to a file, so I don't know much about it.但是,这是我第一次将 OBJECT 写入和读取到文件中,因此我对此知之甚少。 When I try to save the object to the file, it gives me this error:当我尝试将对象保存到文件时,它给了我这个错误:

java.io.NotSerializableException: java.awt.geom.Area
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at Drifter.saveArea(Drifter.java:58)
    at Drifter.keyPressed(Drifter.java:315)
    ...

If anyone can tell me how I can go about writing and reading an object with a file, the help will be greatly appreciated.如果有人能告诉我如何使用文件编写和读取对象,将不胜感激。

TL;DR How do I write the contents of an Area object to a file and read it? TL;DR 如何将Area对象的内容写入文件并读取它?

ALSO I have a few follow-up questions:有一些后续问题:

  • Is ObjectInputStream the best course of action here? ObjectInputStream是这里最好的行动方案吗? I have seen a few answers where people recommend using XML , and JSON , but I can never find the time to learn about them, and would prefer to stick to a pure Java method (without any third party tools)我看到了一些人们推荐使用XMLJSON答案,但我永远找不到时间来了解它们,并且更愿意坚持使用纯 Java 方法(没有任何第三方工具)
  • Is there any other method of saving an object's information to an external source that I can use instead of file handling?是否有任何其他方法可以将对象的信息保存到我可以使用而不是文件处理的外部源?

EDIT - I should also mention that my class implements Serializable编辑- 我还应该提到我的班级实现了Serializable

The exception is pretty self explanatory NotSerializableException: java.awt.geom.Area .异常是不言自明的NotSerializableException: java.awt.geom.Area Any object that you want to serialize must implement the Serializable interface.要序列化的任何对象都必须实现Serializable接口。 java,awt.geom.Area does not. java,awt.geom.Area没有。 Any attributes of that class must also implement Serializable, be a primitive, or be defined as transient.该类的任何属性还必须实现 Serializable,是原始的,或者被定义为瞬态的。

I'd suggest either Figuring out a way to read Area into an object that does implement Serializable .我建议要么找出一种将 Area 读入实现Serializable的对象的方法。 When you read it back out, you can construct a new Area object.当你读回它时,你可以构造一个新的 Area 对象。 This is probably what the JSON/XML method mentioned in the comments is doing.这大概就是评论中提到的JSON/XML 方法正在做的事情。 The added benefit of a human readable storage format is that you can edit it in a text editor.人类可读存储格式的额外好处是您可以在文本编辑器中对其进行编辑。 You won't be able to do that with the binary output of a serialized object`.您将无法使用序列化对象的二进制输出来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM