Last active
June 3, 2019 10:58
-
-
Save Dan6erbond/395311237c1c0d1b0efacdac2c8d9793 to your computer and use it in GitHub Desktop.
A simple method to get all the sub-folders within another folder in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ArrayList<File> getSubFolders(File folder) { | |
File[] dirs = {folder}; | |
ArrayList<File> directories = new ArrayList<>(Arrays.asList(dirs)); | |
File[] fs = {folder}; | |
ArrayList<File> folders = new ArrayList<>(Arrays.asList(fs)); | |
while (folders.size() > 0){ | |
ArrayList<File> remove = new ArrayList<>(); | |
ArrayList<File> add = new ArrayList<>(); | |
for (File f : folders){ | |
File[] subDirectories = f.listFiles((current, name) -> new File(current, name).isDirectory()); | |
directories.addAll(Arrays.asList(subDirectories)); | |
add.addAll(Arrays.asList(subDirectories)); | |
remove.add(f); | |
} | |
folders.addAll(add); | |
folders.removeAll(remove); | |
} | |
return directories; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment