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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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 implementsjava.io.Serializable
. You could do the following:The Preferences API will store the data across sessions. You can find it in the package
java.util.prefs
.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 relatedProperties
object from that file. A nice tutorial on using Java'sProperties
with files can be found here.To get the AppData directory file path (in Windows), you can utilize
System.getenv("APPDATA")
.I would suggest that you create a application specific folder located in the user home directory which you can get with this code:
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 :
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
Use simple database: sqlite or Berkeley DB http://docs.oracle.com/cd/E17277_02/html/index.html