I have a folder (some root folder) in my computer that contains a lot of folders and files. I need to create a String Array that contain all paths (starting from the root folder) of the files (I mean only the leaves = files, not folders). How can I do this?
1 Answer
Using standard Java SE classes and recursion you could do it this way:
import java.io.File;
public class Test {
public static void main(String[] args) {
File root = new File("D:\\Downloaded"); // path to root folder
process(root);
}
private static void process(File path) {
File[] subs = path.listFiles();
if (subs != null) {
for (File f : subs) {
if (f.isDirectory()) {
process(f);
} else {
System.out.println(f.getAbsolutePath());
}
}
}
}
}
Note, instead of System.out.println() you'd probably want to put the path to some ArrayList.
1 Comment
cheziHoyzer
So, shoe me how to put it in ArrayList?