1

I'm creating a game where all locations of 'blocks' are stored in the variable block_area - an object of class 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:

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. 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?

ALSO I have a few follow-up questions:

  • Is ObjectInputStream the best course of action here? 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)
  • 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

Community
  • 1
  • 1
Robo Mop
  • 3,485
  • 1
  • 10
  • 23
  • *"people recommend using XML .. but I .. would prefer to stick to a pure Java method"* The J2SE has built-in support for reading and writing XML. See also [How to serialize Java 2D Shape objects as XML?](https://stackoverflow.com/questions/26579729/how-to-serialize-java-2d-shape-objects-as-xml) – Andrew Thompson Mar 01 '19 at 09:48
  • @Andrew Thompson The thing is, I know nothing about XML, and while usually I'd be in the mood to learn new stuff, this is a simple side project that I don't want to invest too much time in – Robo Mop Mar 01 '19 at 12:13
  • *"I don't want to invest too much time in"* I can relate to that. Moving along .. – Andrew Thompson Mar 02 '19 at 02:58

1 Answers1

2

The exception is pretty self explanatory NotSerializableException: java.awt.geom.Area . Any object that you want to serialize must implement the Serializable interface. java,awt.geom.Area does not. Any attributes of that class must also implement Serializable, be a primitive, or be defined as transient.

I'd suggest either Figuring out a way to read Area into an object that does implement Serializable. When you read it back out, you can construct a new Area object. This is probably what the JSON/XML method mentioned in the comments is doing. 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`.

aglassman
  • 2,643
  • 1
  • 17
  • 30
  • _"I'd suggest either Figuring out a way to read Area into an object that does implement Serializable"_ I am aware of this, and have tried making a class `MA` extending `Area` and implementing `Serializable`, but to no avail. – Robo Mop Mar 04 '19 at 18:33
  • As for the readability part, it's unnecessary because `block_area` will be generated almost randomly, and reading it is of no use to me – Robo Mop Mar 04 '19 at 18:35
  • However, +1 from me for the time you put into this, and it's probably my fault for being too lazy to learn something new like JSOn right now :) – Robo Mop Mar 04 '19 at 18:35
  • Take a look at this: https://stackoverflow.com/questions/2644172/serialize-a-java-awt-geom-area – aglassman Mar 04 '19 at 21:14