Solved Making / Using Alternate Files?

Discussion in 'Spigot Plugin Development' started by ShishKaBobz, Apr 22, 2017.

  1. Hello! Im newer to java / spigotapi and the course i took did not include new files and stuff, So i read the Bukkit Wiki, It does not make total sense, Can someone Help me further?

    Bukkit TUT:
    Code (Text):
    If for whatever reason you need another configuration file, you can use YamlConfiguration to make a new one.

        FileConfiguration data = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "data.yml"));

    This reads the file data.yml from your data folder, and returns a FileConfiguration. If the file does not exist, the FileConfiguration is empty. If you need a pre-made FileConfiguration to be used, you can instead use an InputStream.

        FileConfiguration data = YamlConfiguration.loadConfiguration(getResource("data.yml"))

    This takes the InputStream from getResource, which gets a file embedded in the jar, and retreives a FileConfiguration from that.
     
  2. So what is the actual question?
     
  3. How can i make a new file with code, That doesnt make sense to me, I need someone to explain it more
     
  4. You literally call File::createNewFile(), any hit on Google would've told you that :p (by searching something like "java create file")
     
  5. BananaPuncher714

    Supporter

    You basically use Java itself to create a new file, then turn it into a yaml configuration file.
    Code (Text):
    // To load it:
    File newFile = new File(this.dataFolder(),"locations.yml");
    FileConfiguration secondConfig = Yaml configuration.loadConfigurationFile(newFile);
    // Now you can do whatever with it like a normal config
    // And to save:
    secondConfig.save(newFile);
     
  6. oh watch out, he'll come back and complain over the fact that it wont work
     
  7. BananaPuncher714

    Supporter

    Auto correct messed me up. Im writing this from my phone so plz don't sue me:D
    (It's meant to be "YamlConfiguration")
     
  8. Code (Text):
        public static FileConfiguration data;
        public File datafile;

        public void setup() {
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
            }

            datafile = new File(plugin.getDataFolder(), "data.yml");

            if (!datafile.exists()) {
                try {
                    datafile.createNewFile();
                    Core.plugin.saveResource("data.yml", false);
                }catch (IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml");
                }
            }
            data = YamlConfiguration.loadConfiguration(datafile);
        }

        public void reloadData() {
            data = YamlConfiguration.loadConfiguration(datafile);
        }
     
  9. Why spoonfeed? You know he/she won't learn anything from that.
     
  10. Thank you all! and @jetp250 i looked over the code and i do under stand it :D Thank makes alot of sense! Thanks for the help