Solved Getting names of folders in File array

Discussion in 'Spigot Plugin Development' started by anakinsky249, Jun 13, 2017.

  1. Hi, I need to get the name of every folder. Here's how I'm doing it:
    Code (Text):
    File[] folders = new File(Main.getPlugin().getDataFolder()+"/folders").listFiles(File::isDirectory);
    I need to check if there's a match with a folder name and a given argument (args[2]). Usually, I would have used array.contains() but a file array doesn't have this function. Therefore, I need to convert folder names to a String array.

    Anyone got an idea ?
     
  2. Nevermind, found out myself. Here is how:
    Code (Text):
    File[] folders = new File(Main.getPlugin().getDataFolder()+"/folders").listFiles(File::isDirectory);
                    String[] names = new String[folders.length];
                    for (int i = 0; i < folders.length; i++) {
                           names[i] = folders[i].getName();
                        }

    if(!Arrays.asList(names).contains(args[2])){

      // Stuff here
    }
     
  3. Alternatively you could do something like this...
    Code (Text):
    boolean contains = Arrays.stream(new File(Main.getPlugin().getDataFolder() + "/folders").listFiles(File::isDirectory)).anyMatch(file -> file.getName().equalsIgnoreCase(args[2]));