It will depend on the sort of variables you want to keep, but all methods will require you to write some sort data to a file.
If you only need to keep a handful of variables, you could consider implementing a .config file that could be a simple delimited text file.
If it's an entire object that you want to keep track of, say, a player in an irc game, one option you have is to parse the object into JSON, and save it to a textfile, for reading later. You can use Gson for this
example for a 'player' object:
public String savePlayer(String playerName){ Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create(); String playerFile = System.getProperty("user.dir")+"\\players\\"+playerName; String jsonplayers = gsonPretty.toJson(players.get(playerName)); try{ FileWriter writer = new FileWriter(playerFile+".json"); writer.write(jsonplayers); writer.close(); return "Player file saved successfully!"; }catch(Exception e){ e.printStackTrace(); } return "Something went wrong"; }
you can then create a load method that either has the file name hard coded, or a string input to determine which file to load, and use something like:
playerFromJson = gson.fromJson(jsonString, Player.class);
to use that object in the code