java - Not able to write to AppData -


i'm making small game in javafx , want highscore-system. because i'm not databases, wrote data textfile. easy editable, want write textfile in appdata. tried line:

file file = new file(system.getenv("appdata") + "\\" + [namefoler\\namefile]); 

but keeps saying not have acces folder. writing , off file no problem system works fine if write textfile in same directory jar. keeps throwing these errors.

does know how can prevent this, or better solution problem?

thanks in advance

this answer not appdata, did ask other solutions, 1 based upon preferences api. sample preferences based api storage high-score system. sample not encrypt data.

you mention in question worried user modifying preferences fudge high scores. encrypting or hiding data client application such user not able modify data tricky proposition, won't go how here. if requirement you, can research other info on internet methods accomplish that.

score system

scorestorage.java

import javafx.collections.fxcollections; import javafx.collections.observablelist;  import java.util.prefs.backingstoreexception; import java.util.prefs.preferences;  public class scorestorage {     private static final int max_num_scores = 3;      private static final string score_preference_key_prefix = "score-";      private observablelist<integer> scores = fxcollections.observablearraylist();     private preferences scorepreferences = preferences.usernodeforpackage(         scorestorage.class     );      public scorestorage() throws backingstoreexception {         (string key: scorepreferences.keys()) {             scores.add(scorepreferences.getint(key, 0));         }     }      public observablelist<integer> getunmodifiablescores() {         return fxcollections.unmodifiableobservablelist(scores);     }      public void clearscores() {         scores.clear();         storescores();     }      public void recordscore(int score) {         int = 0;         while (i < max_num_scores && < scores.size() && scores.get(i) >= score) {             i++;         }          if (i < max_num_scores) {             if (scores.size() == max_num_scores) {                 scores.remove(scores.size() - 1);             }             scores.add(i, score);             storescores();         }     }      private void storescores() {         int = 0;         (int score: scores) {             scorepreferences.putint(score_preference_key_prefix + i, score);             i++;         }         while (i < max_num_scores) {             scorepreferences.remove(score_preference_key_prefix + i);             i++;         }     } } 

highscoreapp.java

test harness:

import javafx.application.application; import javafx.geometry.*; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.stage;  import java.util.random; import java.util.prefs.backingstoreexception;  public class highscoreapp extends application {      private static scorestorage scorestorage;      private random random = new random(42);      @override     public void start(stage stage) throws exception {         listview<integer> scorelist = new listview<>(scorestorage.getunmodifiablescores());         scorelist.setprefheight(150);          label lastscorelabel = new label();          button generatescore = new button("generate new score");         generatescore.setonaction(event -> {             int lastscore = random.nextint(11_000);             lastscorelabel.settext("" + lastscore);             scorestorage.recordscore(lastscore);         });          button clearscores = new button("clear scores");         clearscores.setonaction(event -> scorestorage.clearscores());          hbox scoregenerator = new hbox(10, generatescore, lastscorelabel);         scoregenerator.setalignment(pos.baseline_left);          vbox layout = new vbox(10, scoregenerator, scorelist, clearscores);         layout.setpadding(new insets(10));          stage.setscene(new scene(layout));         stage.show();     }      public static void main(string[] args) throws backingstoreexception {         scorestorage = new scorestorage();         launch(args);     } } 

Comments