I’m having trouble loading and saving game data. So I created a folder with a text file. When I click the new button I want to make a new text file and print hp and x and y coordinates.
You could try:
Xml:
http://www.vogella.com/tutorials/JavaXML/article.html
Or
Json:
http://www.tutorialspoint.com/json/json_java_example.htm
Both do the same thing, but I would prefer using xml since it doesn’t require any extra downloading(Its in the java api).
But if your using libgdx, then use the json api that is provided in libgdx.
Or, you could take the long path and learn about java serialization(Not recommended). http://www.tutorialspoint.com/java/java_serialization.htm
Hope this helps! ;D
Simplest way (for what you’re doing): http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file (take into consideration all the answers)
I would go for the json.
What would be the reason to make a binary text file? Would it just print the text in binary code?
Pros and cons of each method:
-
XML/JSON
+very flexible
+easy to edit (for example for debugging)
+easy to implement in OOP way (especially XML)
-takes a lot of space (in comparison with other methods)
-easy to edit (if you are worried that users will edit save data, but you shouldn’t)
-needs some boilerplate code -
Text formats
+very easy to implement
-very dependent on programmer skills and design decisions -
Binary format
+very fast saving/loading
+takes very small amounts of space
+hard to edit (if you are worried that users will edit save data, but you shouldn’t)
-inflexible (in some cases you even need to write a new loader after changing something, in addition to maintaining code for older saves)
-hard to edit (if you want to change some values inside save for debugging or testing purposes)
For most of games, I would choose XML unless you need to save/load a lot of data, then I would choose binary format (but still use XML as alternative for debugging and testing).
In your case even text file should be enough.
Hi
It depends on what you’re going to store, maybe even a simple property file would be enough:
http://docs.oracle.com/javase/tutorial/essential/environment/properties.html