Walk a directory tree recursively Any suggestions are welcome.

package je3.io;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IDEA on 31/01/15.
 */
public class DirWalker {
    private List&lt;File&gt; recursiveList = new ArrayList&lt;File&gt;();

    public void walkDir(String pathname) {
        File d = new File(pathname);
        recursiveList.add(d);
        if(d.isDirectory()) {
            for(String f : d.list()) {
                walkDir(f);
            }
        }
    }

    public void reset() {
        recursiveList.clear();
    }

    public List&lt;File&gt; getRecursiveList() {
        return recursiveList;
    }

    public static void main(String[] args) {
        DirWalker dirWalker = new DirWalker();
        dirWalker.walkDir("/tmp");
        dirWalker.getRecursiveList().forEach(System.out::println);
    }
}
It's simpler to just return the result immediately then need another method.

Also the caller is probably going to need them as File objects eventually so lets just return them as those.

Then there are 2 options, just return a List and use addAll each recursion step:

public List&lt;File&gt; walkDir(File pathname) {
    if(pathname.isDirectory()) {
        List&lt;File&gt; files = new ArrayList();
        files.add(pathname);
        for(File f : pathname.listFiles()) {
            filed.addAll(walkDir(f));
        }
        return files;
    }else{
        return Collections.singletonList(pathname);
    }
}


Or use a private recursive method where you pass in a accumulator List:

private void walkDirRecurse(File pathname, List&lt;File&gt; files) {
    if(pathname.isDirectory()) {
        for(File f : pathname.listFiles()) {
            files.add(f);
            walkDirRecurse(f, files);
        }
    }
}