使用战斧树列出所有文件、目录和子目录
我一直在一个项目中使用 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;
}
更新:代码已更正为评论中提到的。
I've been using tomahawk (1.1.11) for a project. I want to display a tree with all the files and subdirs (and files in those subdirs). I have a code, but it's not listing all of the files and dirs, and don't know where's the mistake.
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;
}
UPDATE: code corrected as mention in comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,终于发现我的错误了!
当我回来时只发现了一个文件。我只是在 for 循环末尾更改 return 。
无论如何,谢谢
Sorry, finally found my error !
I was returning when just one file was found. and I just change that return at the end of the for loop.
Thanks anyway