问题
I'm sort of confused. Let's say I have a game that consists of some tiles and players. All these objects need to be saved so I can later start the game and restore the state. I already do this with simple files like an XML file or plain text files, where I just print the tile type and coordinates into the file and then later read, and parse and create new objects based on the information in the text file.
My question is why should I serialize my objects when I can just save their attributes to a text file? Do many games serialize their objects instea of using text files? Is there any benefit? I could imagine that serializing saves an instance of the object so that you don't have to later create a new instance of the object, like you would have to with a text file. But I really dont know!
回答1:
The main benefit of serialization in java is that you don't have to manually do the reading/writting into files, as you are doing now. You just need to implement the Serializable interface and java takes care of it.
Here's a nice tutorial of using serialization in Java: http://www.tutorialspoint.com/java/java_serialization.htm
回答2:
- You have a version controll via UID.
- Its Fast.
- You dont need extra code.
回答3:
I guess serialization will write content in binary format, hence it will consume less space than the corresponding text
you can directly get java object from serialized file
static variable state will not be saved when using serialization
来源:https://stackoverflow.com/questions/19362893/java-serialization-vs-plain-text-file