Solved Extracting embedded YAML files

Discussion in 'Plugin Development' started by Tietsap, Apr 8, 2014.

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

    Tietsap

    I have a plugin which is supposed to use custom named YAML files (for example database.yml or general.yml) to load and save configuration. I have these YAML files embedded in my plugin in the default package folder (the same folder where config.yml and plugin.yml are located). Is there a way to extract these custom YAML files like bukkit would do with config.yml? I've looked at the documentation for getResource(), but I still remain unsure of how to use this. If anyone could provide me with an example I would be forever grateful.

    Thanks in advance.

    For those who are trying to tackle this same issue, I have found a solution that works for me:
    Code:java
    1. try {
    2. InputStream inputStream = getClass().getResourceAsStream("filename.yml");
    3. FileOutputStream fileOutputStream = new FileOutputStream(new File(plugin.getDataFolder(), "filename.yml")));
    4.  
    5. Integer read = 0;
    6. byte[] bytes = new byte[1024];
    7. while ((read = inputStream.read(bytes)) != -1) {
    8. fileOutputStream.write(bytes, 0, read);
    9. }
    10. } catch (IOException ex) {
    11. plugin.getLogger().warning("Error: An error occurred while trying to create the config file (" + ex.getMessage() + ")");
    12. }

    Keep in mind that using this solution, the YAML files will have to be in the same folder as the class that loads them. I have yet to find a solution to get this to work with the files in the default package.

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

    iPoke111

    Tietsap
    sorry if this is a bump, but
    thanks for setting your thread to solved, nobody else does it
     
    Tietsap likes this.
  3. Offline

    Tietsap

    Isn't it a rule on this forum?
     
  4. Offline

    iPoke111

    Tietsap
    possibly but still nobody does it :L
     
Thread Status:
Not open for further replies.

Share This Page