Store local data in java? [closed]

笑着哭i 提交于 2019-12-13 10:47:53

问题


I am using Java to make a simple text adventure. I want to be able to define progress each mission, and that will store somewhere in the user's appdata to be read next time they play the game. How can I do this?


回答1:


If you just want to store the data internally (i.e., save across sessions, but not as a user-readable file), I would use the Preferences API.

For example: consider that you have a class called MissionInfo which implements java.io.Serializable. You could do the following:

// Precondition: missionInfoToSave is an variable of type MissionInfo

// The key used to store the data.
final String key = "SAVE_DATA";

// Get the preferences database for this package.
Preferences prefs = Preferences.userNodeForPackage(MissionInfo.class);

// To save, write the object to a byte array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(missionInfoToSave); // write it to the stream
    prefs.putByteArray(key, baos.toByteArray());
} catch (IOException ie) {
    System.err.println("Failed to save to file");
}

// To load, read it back.
// The second argument is the default if the key isn't found.
byte[] stored = prefs.getByteArray(key, null);
if (stored == null) {
    // There's no stored data.
    return;
}
ByteArrayInputStream bais = new ByteArrayInputStream();
try {
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object o = ois.readObject();
    if (o instanceof MissionData) { 
        // Good: it's a saved data file.
        updateMissionProgress((MissionData) o); // assuming this is defined
    }
} catch (IOException ie) {
    System.err.println("Couldn't load from prefs");
} catch (ClassNotFoundException cnfe) {
    System.err.println("Class couldn't be found");
}

The Preferences API will store the data across sessions. You can find it in the package java.util.prefs.




回答2:


Use simple database: sqlite or Berkeley DB http://docs.oracle.com/cd/E17277_02/html/index.html




回答3:


I would suggest that you create a application specific folder located in the user home directory which you can get with this code:

String userHome=System.getProperty("user.home");

That will give you a string to something like c:\Documents and Settings\user1 and then you can append to that string your appname like "adventureapp" with a forwardd slash first. Then from that. You can do this :

File file = new File(mypathtomyappfolder);

Then you test if .isDirectory() if not, it is the first time user is running app do file.newDir() so you have your adventureapp directory. Then from there use File and append a file name to your string like user.properties which you can read and write to based on their progression in the game. Hope that helps. - Duncan




回答4:


You can make use of java.util.Properties, which is a convenient utility for creating a map of keys and values, storing it in a file, and loading a related Properties object from that file. A nice tutorial on using Java's Properties with files can be found here.

To get the AppData directory file path (in Windows), you can utilize System.getenv("APPDATA").



来源:https://stackoverflow.com/questions/13484549/store-local-data-in-java

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