ini4j and libgdx, read and write data

732 Views Asked by At

Hello Im trying to save the language save by the user in my game I have made a test to see if the script create the file and read it good.

Im using ini4j to store the value of language in a ini file.

I have no file, on the 1st start, programm create the empty file in the Local directory and fill it with value (language=fr) for test.

On 2nd start, the programm check if the file exist, then open it to read the value

The problem is that the 'lang' variable seems to be empty when it read it by my function sic_Language.setLanguage(String text);

My code

String lang;
Wini ini;
    //Read
    if (Gdx.files.local("mygame.ini").exists()) {

        //Open file
        try {
            ini = new Wini(Gdx.files.local("mygame.ini").file());
        } catch (InvalidFileFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Read values
        lang = ini.get("interface", "language");
        sic_Language.setLanguage(lang); //Set language

    }
    //Write
    else {

        //Create file
        try {
            Gdx.files.local("mygame.ini").file().createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Open file
        try {
            ini = new Wini(Gdx.files.local("mygame.ini").file());
        } catch (InvalidFileFormatException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        //Write values
        ini.put("interface", "language", "fr");

        try {
            ini.store();
        } catch (IOException e) {
            e.printStackTrace();
        }

        sic_Language.setLanguage("fr"); //Set language
    }
0

There are 0 best solutions below