【问题标题】:List all files, dirs and subdirs with tomahawk tree使用战斧树列出所有文件、目录和子目录
【发布时间】:2011-12-24 16:57:42
【问题描述】:

我一直在使用 tomahawk (1.1.11) 进行项目。我想显示一个包含所有文件和子目录(以及这些子目录中的文件)的树。我有一个代码,但它没有列出所有文件和目录,不知道哪里出错了。

public TreeNode getTreeData() {
    path = loadConfiguredPath();
    String dependencia = userVerifier.getDependencia();

    if (dependencia.equals("TEST")) {
        path = path + "dataFiles";
    } else {
        path = path + "dataFiles\\" + dependencia;
    }

    dirRoot = new File(path);
    treeRoot = new TreeNodeBase("folder", "BASEDIR", false);
    createTree(dirRoot, treeRoot);

    return treeRoot;
}

private void createTree(File fileRoot, TreeNode treeRoot) {
    File[] files = fileRoot.listFiles();
    TreeNodeBase tnb;
    for (File f : files) {
        if (f.isDirectory()) {
            tnb = new TreeNodeBase("folder", f.getName(), false);
            treeRoot.getChildren().add(tnb);
            createTree(f, tnb);
        }
        if (f.isFile()) {
            tnb = new TreeNodeBase("file", f.getName(), false);
            treeRoot.getChildren().add(tnb);
            //return;
        }
    }
    return;
}

更新:代码已更正为评论中提及的内容。

【问题讨论】:

  • 对不起,终于发现我的错误了!当只找到一个文件时,我正在返回。我只是在 for 循环结束时更改了 return 。还是谢谢。

标签: recursion tree tomahawk


【解决方案1】:

抱歉,终于发现我的错误了!

当我只找到一个文件时,我正在返回。我只是在 for 循环结束时更改 return

还是谢谢

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 2012-09-02
    • 2014-09-12
    • 1970-01-01
    • 2011-03-01
    • 2017-05-01
    • 2016-01-16
    相关资源
    最近更新 更多