java - How do I save states to a file and keep the file safe? -


i'm trying create save states game, not game left simple score boards. format this:

wins: 5 losses: 10 gamesplayed: 15 

i need able access file, , depending on whether player won/lost append +1 value in file.

what best way go this? i've heard of bunch of different ways save data, example xml, aren't overkill size of data?

also, want keep file safe users being able go files , change data. have sort of encryption? and, if user removes file , replaces empty 1 can't technically reset values?

you can use plain serialization/deserialization this. in order serialize/deserialize class, must implement serializable interface. here's example start with:

public class score implements serializable {     private int wins;     private int loses;     private int gamesplayed;     //constructor, getter , setters... }  public class scoredatahandler {      private static final string filename = "score.dat";     public void savescore(score score) {         objectoutputstreamout = null;         try {             out = new objectoutputstream(new fileoutputstream(filename));             out.writeobject(score);         } catch (exception e) {             //handle exceptions...         } {             if (out != null) {                 try {                     out.close();                 } catch (ioexception ioe) {                 }             }         }     }      public score loadscore() {         objectinputstreamin = null;         score score = null;         try {             in = new objectinputstream(new fileinputstream(filename));             score = (score)in.readobject();         } catch (exception e) {             //handle exceptions...         } {             if (in != null) {                 try {                     in.close();                 } catch (ioexception ioe) {                 }             }         }         return score;     } } 

Comments

Popular posts from this blog

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -