I got an annyoing problem that I cant solve regarding saving/reading files in Libgdx. I have a “GameData” class which I can save by using the JSON parser. Everything is working fine on the desktop but when I package it and run it from a jar everything stops working. I worked by using this mannual: https://github.com/libgdx/libgdx/wiki/File-handling.
Currently I am using local files which should be readable and writable instead of internal files (which I use just for stuff which are readable only like gameStrings) and would like to know what I am doing wrong and how I can change my code so it would run on android as well.
Thanks in advance for your help:
public class SaveHandle {
private GameData gameData;
private GameStrings gameStrings;
private String gameSavePath = "gameSave.json";
private String stringSaveEn = "save/strings_en.json";
private String stringSaveDe = "save/strings_de.json";
private FileHandle file;
private Json json;
public SaveHandle() {
json = new Json();
}
public void init() {
}
public void save(GameData gameData) {
file = Gdx.files.local(gameSavePath);
file.writeString(json.prettyPrint(gameData), false);
}
public GameData load() {
file = Gdx.files.local(gameSavePath);
try {
if (json.fromJson(GameData.class, file) != null) {
gameData = json.fromJson(GameData.class, file);
}
} catch (Exception e) {
System.out.println("exception");
init();
}
if (gameData == null) {
gameData = new GameData();
gameData.init();
}
return gameData;
}
public GameStrings loadStrings() {
if (gameData.getLanguage().equals(Language.English)) {
file = Gdx.files.internal(stringSaveEn);
} else {
file = Gdx.files.internal(stringSaveDe);
}
gameStrings = json.fromJson(GameStrings.class, file);
return gameStrings;
}
public void reset() {
GameData newData = new GameData();
newData.init();
save(newData);
}
}