PDA

View Full Version : Good way to store and load Data in Java?


UrbanTwitch
09-27-2009, 09:35 PM
Let's say I want to make an RPG in Java.. (console application) and I want to save the user's input of Name, level, exp, gold,... what would be the best way I can save that data and load it for use in Strings and such?

Right, at the moment, I am using Textfiles... but if I want to append a textfile like... save the new number for gold... I'd have to write on ALLL lines like

// more code...
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object


try { out = new FileOutputStream("stats.txt");
p = new PrintStream( out );
p.println(name);
p.println("500"); // gold
p.println("1"); // level
p.println("1"); // exp
p.println("1"); // room
p.println("100"); // current hp
p.println("100"); // max hp
p.close();
}catch (IOException ioe)
{
System.out.println("An unexpected error occured.");
}//end catch errors
// more code...


See what I mean? Is there a way to append a line of text to a single line of text like... append "Boo?" to line number 3 in stats.txt or maybe replace line 2 with "1242"?

Fou-Lu
09-29-2009, 12:11 AM
What you're describing is a RandomAccessFile. Though there is a problem: p.println(name);. Name is a string, which means that it has a variable size. In a RandomAccessFile, you'd need to change that to be a particular length so you can use the seek method to move say 50 bytes to get to the next variable.

For an easier route (though worse on memory and data storage space), you could instead implement Serializable on any object you want. This will let you read and write an object to a file instead, and you can easily update it by just changing the property.
Aaannnddd... a serialization article for you :)
http://java.sun.com/developer/technicalArticles/Programming/serialization/

UrbanTwitch
09-29-2009, 02:35 AM
Hm thanks! i tried one of those examples and I got this error when I tried to compile. :S

import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
public class InflateTime
{
public static void main(String [] args)
{
String filename = "time.ser";
if(args.length > 0)
{
filename = args[0];
}
PersistentTime time = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try
{
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
time = (PersistentTime)in.readObject();
in.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
// print out restored time
System.out.println("Flattened time: " + time.getTime());
System.out.println();
// print out the current time
System.out.println("Current time: " + Calendar.getInstance().getTime());
}
}
C:\Users\Dan\Documents\Java\InflateTime.java:14: cannot find symbol
symbol : class PersistentTime
location: class InflateTime
PersistentTime time = null;
^
C:\Users\Dan\Documents\Java\InflateTime.java:21: cannot find symbol
symbol : class PersistentTime
location: class InflateTime
time = (PersistentTime)in.readObject();
^
2 errors

Er... what?

Fou-Lu
09-29-2009, 03:22 AM
PersistentTime is not a built in class in java; its the first class defined in their example.
Test it using a predefined java class first, ArrayList is a good one to go with, its serializable and will show you that collections allow serialization. Offhand, I can't recall if each item is also required to be serializable (ie: <T extends Serializable>), or if it can go ahead and serialize T without it (the API lists that it uses E, not <E extends Serializable> so I would presume that its not required). That being said, use a primitive wrapper like Integer which is also serializable.