Skip to content

Instantly share code, notes, and snippets.

@kineticz
Created January 31, 2015 21:40
Show Gist options
  • Select an option

  • Save kineticz/98ff3bee21a8ba39bcf7 to your computer and use it in GitHub Desktop.

Select an option

Save kineticz/98ff3bee21a8ba39bcf7 to your computer and use it in GitHub Desktop.
Correct version of directory walker.
package je3.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IDEA on 31/01/15.
*/
public class WalkDir {
private List<File> recursiveList = new ArrayList<File>();
public void walkDir(String pathname) {
File d = new File(pathname);
recursiveList.add(d);
if(d.isDirectory()) {
for(String f : d.list()) {
walkDir(f);
}
}
}
public List<File> getRecursiveList() {
return recursiveList;
}
public static void main(String[] args) {
WalkDir dirWalker = new WalkDir();
dirWalker.walkDir("/tmp");
dirWalker.getRecursiveList().forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment