I don't know where this question came from, but... Is there a way to start at one directory, say... C:\ . and I wanted to go into that folder, and printout a list of the folders/files in that directory, and then do the same for the rest of the folders that exist for so many levels down?

I want it to look something like this:

--> Documents and Settings
----> All Users
------> // files/folders in All Users
----> User1
------> // files/folders in User1
----> // files in Documents and Settings
--> // other folders/files

I'm just curious if something like that is possible, and if so, how I would start something like that. I know it would end up being recursive, and that it would be useful to ask for a starting location and depth to check, but other than that, I have no idea where to start.

8 Years
Discussion Span
Last Post by llemes4011

Here's a skeletal recursion for it. You just need to figure out the depth part

public void listFiles(File dir){
    for (File file : dir.listFiles()){
        if (file.isDirectory()){
        } else {
            // whatever

I got the code to work, but how does that for loop function? I've seen the same thing done with String objects, but I've never used/understood them... i've only used the: for(int z=0;z<#;z++) structure....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.