How can I save data from previos runs?
I need to save the data of object "player" that has the fields:
String name
int health
int strength
int speed
I might create more fields
p.s. I'm a beginner, so please make it as simple as possible :confused:
Printable View
How can I save data from previos runs?
I need to save the data of object "player" that has the fields:
String name
int health
int strength
int speed
I might create more fields
p.s. I'm a beginner, so please make it as simple as possible :confused:
Where do you want to save the data? In what format? How will you retrieve that data later?Quote:
I need to save the data of object "player" that has the fields:
For example:
If you write the data to a file, do you want the contents of the file to be text or can some of it be binary?
I'm not sure.
is there some sort of code that cant automatically create a file that saves the data and one that automatically retrives it?
Look at the ObjectOutputStream for a way to write the contents of an object. And ObjectInputStream to read it back.
I am not sure, but can the serialization help in this case?
The easiest way for beginners to do this would be to output all the objects to a text file before the program closes.
Since your Player class only has 3 attributes, that would be relatively easy to store.
Google "How to read/write to a text file java" and you will find many examples on how to do this.
What that will do is write those 3 attributes to a text file then when your program opens up again, it will read from that same file.