Java Serialization vs Plain text file [closed]

陌路散爱 提交于 2020-06-28 07:20:14

问题


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:


  1. You have a version controll via UID.
  2. Its Fast.
  3. You dont need extra code.



回答3:


  1. I guess serialization will write content in binary format, hence it will consume less space than the corresponding text

  2. you can directly get java object from serialized file

  3. static variable state will not be saved when using serialization



来源:https://stackoverflow.com/questions/19362893/java-serialization-vs-plain-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!