Java尾递归文件夹遍历

846 查看

public void Traverse(File dir) throws IOException {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; children != null && i < children.length; i++) {
           Traverse(new File(dir, children[i]));
        }
    }

    if (dir.isFile()) {
        // do whatever you want
    }
}

参考资料:
[1]什么是尾递归?http://en.wikipedia.org/wiki/Tail_recursion