Solved One files saves correctly, the other doesn't

Discussion in 'Plugin Development' started by Randy Schouten, Jan 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Randy Schouten

    So, as the title says, one files saves the data correctly, and another one doesn't.

    The one that saves correctly:
    Code:
    String playername = playersToSave.get(i);
    File savefile = new File("plugins" + File.separator + "EpicQuest" + File.separator + "Players" + File.separator + playername + ".yml");
     
    //Reset the file by recreating the file
    if(!savefile.exists()){
    savefile.createNewFile();
    }else{
    savefile.delete();
    savefile.createNewFile();
    }
     
    //Make the file editable
    FileConfiguration save = YamlConfiguration.loadConfiguration(savefile);
    The one that doesn't save correctly:
    Code:
            //Reset the file by recreating the file
    File signfile = new File("plugins" + File.separator + "EpicQuest" + File.separator + "signs.yml");
                FileConfiguration sign = YamlConfiguration.loadConfiguration(signfile);
             
                if(!signfile.exists()){
                    signfile.createNewFile();
                }else{
                    signfile.delete();
                    signfile.createNewFile();
    sign.save(signfile);
                }
     
    //Reload the file
                sign = YamlConfiguration.loadConfiguration(signfile);
    As you can see, the method I use is the same, really.
    The thing is that "sign" doesn't reload and it just adds/changes stuff that's already in the file, it doesn't start with a blank file.

    I've also tried the "sign.set("Signs", null);" way, same results.
    Same thing for moving the whole File and FileConfiguration to the top, as I need the file twice (once for loading, once for saving).

    Normally I can figure out what is wrong, this has me bummed though...

    Any idea?

    Nevermind, trying a few different things and one seemed to solve the issue ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    fireblast709

    you load the config, empty it, save the loaded config (so it is back to what it was) and load the config again...

    [edit] you seem to already figured it out
     
  3. Offline

    nisovin

    The thing is, those are NOT the same. The second one does this:

    1. Load the file into memory
    2a. If file doesn't exist, create it
    2b. If it already exists, delete it, re-create it, then save the file in memory back to disk
    3. Load the file again

    See the problem? Your first example doesn't have this problem, because it doesn't have that extra file load that the second one does.
     
  4. Offline

    Randy Schouten

    Yeah that was what I was going for the whole time, I understood how it has to be done, it just didn't work for some reason.
    Anyway, it does work now with the least amount of code, so yeah.
    Thanks for responding anyway ;)

    Yep, I saw that and moved a few things around and got it working anyway.
    Thanks for responding anyway!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page