How can I only get 1 file from a File List

Discussion in 'Spigot Plugin Development' started by kinukin, Jun 4, 2017.

  1. Code (Text):
    public static void getConfig(String name) {
            File[] files = new File(Main.plugin.getDataFolder().getAbsolutePath()).listFiles();
            for (File file : files) {
                if (file.isFile()) {
                   name = file.getName();
                    customFile = new File(Main.plugin.getDataFolder() , name);
                    customConfig = YamlConfiguration.loadConfiguration(customFile);
                    if(name.equalsIgnoreCase(customFile.getName())) {
    I'm trying to only get 1 file from a file list. At the moment it's getting all of the files. How can I do this?

    Thank you
  2. Just get the file you want instead of looping over the whole array? Or are you trying to get a specific file by name? If so, just make a new Path or File object pointing to it.

    This looks like an "i dont know any java and am copy-pasting randomly" question, honestly..
    • Agree Agree x 1
  3. isFile method make no sense, you should compare file.getname with another string with your required config
  4. Code (Text):
    File f = new File(plugin.getDataFolder(), "config.yml");
    YamlConfiguration config = YamlConfiguration.loadConfiguration(f);