0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So, shoe me how to put it in ArrayList?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.