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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
Last edited by bardd; September 19th, 2010 at 06:59 AM.
a good programmer can write a Java program in any language
Where do you want to save the data? In what format? How will you retrieve that data later?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?
a good programmer can write a Java program in any language
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.