# How do I save files with libgdx so that users can't read them?

Writing my game in libgdx, I arrived at the point when I need to save the player stats and the info of the levels. However, in libgdx it's not allowed to write the file inside folder of the application, only external (on the SD) is allowed. The point is that I don't want the file to be seen by anyone, or if they can see it, how can I convert it to a binary file so it's not human readable? I just want to hide the file.

• Binary files are human readable too, it just requires a sufficiently skilled and motivated human. – Adam Sep 15 '13 at 0:11

Libgdx has built in decoding and encoding functionality. Gustavo Steigert has written a nice article about this on his blog: http://steigert.blogspot.be/2012/03/5-libgdx-tutorial-files.html. Scroll down a bit to the section "2. Persist Profile operation".

In this blogpost, he explains that by using the class com.badlogic.gdx.utils.Base64Coder you can encode and decode files in the Base64-format. Leaving the unreadable and unmodifiable for end users.

• While better than plaintext base64 is visually recognisable and web based en/decoding tools are a search away. – Kelly Thomas Apr 16 '13 at 20:53

It sounds like using a "preference" is what you need to do. It allows you to save small bits of data (like a high score, or other settings) which persist when the game is shut down.

And it's a lot easier than trying to write to a file; you don't really have to concern yourself where the file is.

On Windows it gets put in the user's home directory. Android uses a SharedPreference system.

If you don't want users being able to read what's in there, you can always save your values in an encoded way. Ex:

prefs.putInteger("HighLevel", 15);


or

prefs.putString("SecretInfo", myEncodeFunction("here's the secret text to save."));


The following code will create a text file where you can store basic data

FileHandle file= Gdx.files.local("files/myfile.txt");
file.writeString("my_first_file", false);


FileHandle file= Gdx.files.local("files/myfile.txt");


now

    if (text.equals("my_first_file")) {
}


if you'd like to check first whether not the file exits,

first, initialize :

boolean my_file_exits = Gdx.files.local("files/myfile.txt").exists();


then

  if (exists) {
FileHandle file= Gdx.files.local("files/myfile.txt");

if (text.equals("my_first_file")) {
}
} else {
//create a textfile and store default value
}


Basically, you may want to read up on how Android handles directories in your app. There is a file handle class in libgdx that should work for you. If you just want it saved in a protected place, use the internal process and it should be local to your app directory and not visible. It doesn't have to be on an sd card. Read the android docs and ask on the badlogic forum if you still need help and don't get any answers here

• libgdx doesnt let you write files internally, i resolved it making a callback to a method in Android, because Android can write it. Thanks – Rudy_TM Feb 17 '12 at 15:16
• Read this badlogicgames.com/wordpress/?p=2305 I believe it could be what you need. – arielsan Mar 19 '12 at 2:55
• @Rudy_TM please add your comment as an answer and select it as the accepted answer so other people who read this later can clearly see it. – ashes999 Apr 2 '12 at 10:57

I resolved it with callbacks, with that I can use Android methods, that way I can write to the internal memory.

• You should show what what callbacks you used. It'll be more useful for future visitors. – MichaelHouse Apr 2 '12 at 19:54
• Does the Internal file type not solve your problem? code.google.com/p/libgdx/wiki/FileModule – Chuck D Apr 2 '12 at 20:08
• You actually shouldn't use callbacks unless you don't want to use latest LibGDX... "local" files have been added as mentioned on blog to allow saving to private app directory – XiaoChuan Yu Apr 3 '12 at 0:14
• I didnt know, i did the code like 2 months ago :/ i will change it :) thaks – Rudy_TM Apr 3 '12 at 20:46
• @RubberMallet no, that doesnt do the trick, because it only reads, it doesnt write, see the part of the blog that put XiaoChuan Yu, thats the new stuff for files (its of type local) :) – Rudy_TM Apr 3 '12 at 20:49