0

Nice and easy question.. Im using filehandle to write and read scores.. wins and lossess.. but unlike a buffered reader you cant use read.line(); so how do you write and read lines in filehandler?? thanx here is the code

    private void writeSaveData()
{
    winstring = Integer.toString(wins);
    lossstring = Integer.toString(losses);

    FileHandle scores = Gdx.files.local("Score");
    scores.writeString(winstring, false);
    // I want to add another line here for lossess
}


private void loadScores()
{
    FileHandle scores = Gdx.files.local("Score");
    winstringread= scores.readString(winstring);
    wins = Integer.parseInt(winstringread);
    // same here add line to read lossess
}
Joe
  • 65
  • 1
  • 8
  • My answer on another post will help you reading a whole file into a multiple lines, writing it to new lines is the same, but appending "\r\n" to each String you want to be on a separate line [Libgdx file handle.. reading a single line](http://stackoverflow.com/questions/37281063/libgdx-file-handle-reading-a-single-line/37281292#37281292) – Mr00Anderson May 17 '16 at 19:55
  • That was my question too, it doesnt say how to write on two lines? any help.. – Joe May 17 '16 at 21:51
  • 1
    When you create a string just add + "\r\n" before saving the whole thing out.`String output = String.format("Wins: %d\r\nLosses: %d", wins, losses);` – Mr00Anderson May 17 '16 at 23:09
  • hey Underbalanced... still need your help please man... i can write the file but not read it.. sigh.. this is the last thing and im stuck reading two lines ffs.. please help how do you read the file??? – Joe May 19 '16 at 19:34
  • Do you want the file to be readable to a user or only the program? – Mr00Anderson May 20 '16 at 14:11

2 Answers2

1

For libgdx and this specific problem you are describing. You can read and write the strings whole and parse on delimiters.

Non Human Readable. What we are doing here is taking and using String format so we can clearly see how the line will be written. We are making the winning score first and losses second, So if I win 10 and they lost 5 the string will look like this 10=5. This will be save into the file. When we read the file back we read 10=5 and then split this string up on =and are left with two strings 10 and 5. We can then parse these to integers.

private void writeSaveData()
{
    String saveString = String.format("%d=%d", wins, losses);
    FileHandle scores = Gdx.files.local("Score");
    scores.writeString(saveString, false);
}


private void loadScores()
{
    FileHandle scores = Gdx.files.local("Score");
    String loaddedString = scores.readString(winstring);
    String[] scores = loadedString.split("=");
    wins = Integer.parseInt(scores[0]);
    losses = Integer.parseInt(scores[1])    
}
Mr00Anderson
  • 854
  • 8
  • 16
  • thank you my friend.. again perfect an easy answer with no complications.. i understand what your saying i tried this but didnt work ... nooby %d\r\n%d.. guess cause there is probably some string code in there.. thanx again for the help... – Joe May 20 '16 at 20:27
  • also the 0 and 1 indexing is alooot of help because i started with 1.. :( – Joe May 20 '16 at 20:27
  • @Joe no problem man! I am building a Sandbox MMO, like Terreria/Starbound using libGDX i can understand the frustrations of API limitations. So I usually am finding ways around too! – Mr00Anderson May 20 '16 at 22:03
  • yeah man.. coding can be really frustrating knowing there is a code out there to help you that you dont know... but so worth it the moment that you hit run and it works.. : ) .. gdluck on your game man hope its a success.. – Joe May 22 '16 at 15:35
0

In your case I would suggest you to take a look at JSON format. Here is a official docs. Basically you just need to create simple POJO class, in your case it could be something like:

public class GameResults {

   private int wins;
   private int losses;
   ...
   public int getWins() {
       return wins;
   }
   public void setWins(int wins){
       this.wins=wins; 
   }
  ...

}

Then whenever you need to write to result file you use:

GameResults results = new GameResults();
results.setWins(winstring);
Json json = new Json();
FileHandle scores = Gdx.files.local("Score");
scores.writeString(json.toJson(results), false);

And when you need to read:

FileHandle scores = Gdx.files.local("Score");
winstringread = scores.readString(winstring);
Json json = new Json();
GameResults results = json.fromJson(GameResults, winstringread);

Bonus:

Also, you can use Base64Coder class to decode your result file. Just to be a little more protected from hacks. All you need is to add this line when you read file:

Base64Coder.decodeString(RESULT_FILE.readString()), false)

and that line when you write:

RESULT_FILE.writeString(Base64Coder.encodeString(RESULT_FILE.readString()), false);
Enigo
  • 3,685
  • 5
  • 29
  • 54