Saving Loading files - JustKato/BukkitWiki GitHub Wiki

Saving to files

The preffered format is YML, as we have a really good bukkit API that helps us use YML files. You can always make your own file format and name it whatever and save it however you want, but serialization is sooo much easier using YML, here's some examples.

Saving just a line to a file ( The correct way not the File way )

public void SavePlayerCounter(Player p, String value) {
    File folder = new File("plugins" + File.separator + "YOUR PLUGIN NAME" + File.separator );
    File EconomyDatabase = new File(folder + File.separator + "FILENAME.yml");
    YamlConfiguration yaml = new YamlConfiguration();

    yaml.createSection(p.getUniqueId() + ""); // Create a section with this player's name
    yaml.set(p.getUniqueId() + ".points", value); // save the points under the player's name
    
    // Actually save the thing
    try {
        yaml.save(EconomyDatabase);
    } catch (Exception ex ) {
        ex.printStackTrace();
    }
}

Final:

JustKato:
    - points: 3

saving more than just a line

public boolean SaveToFile() {
    File folder = new File("plugins" + File.separator + "YOUR PLUGIN NAME" + File.separator );
    File EconomyDatabase = new File(folder + File.separator + "FILENAME.yml");
    YamlConfiguration yaml = new YamlConfiguration();

    // Save the fictional accounts to file
    yaml.createSection("Accounts"); // This will create a section in your YML file
    // Sections are super cool because you can basically yml.load("section"); :)

    // saving to the section
    // please note the . after Accounts "Accounts.", we are adding the . because
    // that signals we are INSIDE the section ( look at the finished product to
    // see what we're talking about )
    yaml.set ("Accounts." + "first", "value");
    yaml.set ("Accounts." + "second", "second");

    // Actually save the thing
    try {
        yaml.save(EconomyDatabase);
    } catch (Exception ex ) {
        ex.printStackTrace();
    }

    return true;
}

The result should look something like this:

Accounts:
    - first : 'value'
    - second: 'second'

Loading

Loading from your files it's a little bit more complex, and a little bit more annoying. It really depends on the way you structured your stuff, right now we just want to get 2 stupid String variables. I will update the guide with saving ItemStacks and/or Locations, players.

public Map<String, String> LoadFromFile() {
    File folder = new File("plugins" + File.separator + "PLUGIN NAME");
    File EconomyDatabase = new File(folder + File.separator + "FILE NAME.yml");
    YamlConfiguration yaml = new YamlConfiguration();

    // Load the file and read it.
    try {
        yaml.load(EconomyDatabase);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    
    // This will return a memory section with all your values
    MemorySection Accounts = (MemorySection) yml.get("Accounts") 

    Set<String> keys = Accounts.getKeys(false); // The parameter is if it should go deep on searching for keys, but no.
    Map<String, String> finals = new HashMap<>(); // This will store our final result
    for ( String key : keys )
        finals.put(key, Accounts.getString(key, Accounts.getString(key);
    
    return finals;
}
⚠️ **GitHub.com Fallback** ⚠️