英文:
How to obtain directories and subdirectories and group them by parent and children
问题
以下是您要求的翻译部分:
我想要从文件夹中获取所有目录和子目录,并根据父目录和子目录对它们进行分组。
我使用以下代码获取了所有目录和子目录:
String rootFolder = "/Users/user/Desktop/testFolder";
try (Stream<Path> paths = Files.walk(Paths.get(rootFolder))) {
List<String> pathStr = paths.filter(Files::isDirectory)
.map(path -> path.toString())
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
结果如下所示:
/Users/user/Desktop/testFolder
/Users/user/Desktop/testFolder/folder1
/Users/user/Desktop/testFolder/folder1/folder1
/Users/user/Desktop/testFolder/folder2
/Users/user/Desktop/testFolder/folder2/folder1
/Users/user/Desktop/testFolder/folder3/folder1/folder1
对于下一步,我想将它们组织成如下所示:
{
"path" : "/Users/user/Desktop/testFolder",
"children" :
[
{
"path" : "/Users/user/Desktop/testFolder/folder1",
"children" :
[
{
"path" : "/Users/user/Desktop/testFolder/folder1/folder1",
"children" : []
}
]
},
{
"path" : "/Users/user/Desktop/testFolder/folder2",
"children" :
[
{
"path" : "/Users/user/Desktop/testFolder/folder2/folder1",
"children" : []
}
]
},
{
"path" : "/Users/user/Desktop/testFolder/folder3",
"children" :
[
{
"path" : "/Users/user/Desktop/testFolder/folder3/folder1",
"children" :
[
{
"path" : "/Users/user/Desktop/testFolder/folder3/folder1/folder1",
"children" : []
}
]
}
]
}
]
}
我已经准备好了以下类:
public class PathTree {
private String path;
private List<PathTree> children;
}
我知道使用循环并逐一检查所有这些路径前缀最终可以实现我想要的结果,但我想知道是否有其他替代方法或解决方案。
英文:
I want to obtain all the directories and subdirectories from folder and grouping them based on parent and children.
I obtained all directories and subdirectories with the following codes :
String rootFolder = "/Users/user/Desktop/testFolder";
try (Stream<Path> paths = Files.walk(Paths.get(rootFolder))) {
List<String> pathStr = paths.filter(Files::isDirectory)
.map(path -> path.toString())
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
The results was like this :
/Users/user/Desktop/testFolder
/Users/user/Desktop/testFolder/folder1
/Users/user/Desktop/testFolder/folder1/folder1
/Users/user/Desktop/testFolder/folder2
/Users/user/Desktop/testFolder/folder2/folder1
/Users/user/Desktop/testFolder/folder3/folder1/folder1
For the next step I would like organise them into something like :
{
path : "/Users/user/Desktop/testFolder",
children :
[
{
path : "/Users/user/Desktop/testFolder/folder1",
children :
[
{
path : "/Users/user/Desktop/testFolder/folder1/folder1",
children : []
}
]
},
{
path : "/Users/user/Desktop/testFolder/folder2",
children :
[
{
path : "/Users/user/Desktop/testFolder/folder2/folder1",
children : []
}
]
},
{
path : "/Users/user/Desktop/testFolder/folder3",
children :
[
{
path : "/Users/user/Desktop/testFolder/folder3/folder1",
children :
[
{
path : "/Users/user/Desktop/testFolder/folder3/folder1/folder1",
children : []
}
]
}
]
}
]
}
I have the following class ready :
public class PathTree {
private String path;
private List<PathTree> children;
}
I aware that using loop and check all those path prefix one by one should eventually achieve the result I want, but I would like to know if there is any alternative or solution.
答案1
得分: 0
你可以使用递归方法编写以下伪代码:
PathTree createTree(String root) {
PathTree root = new PathTree(root);
Files.walkFileTree(root, options, labda_add_children_to_root_above);
root.children.foreach(c -> createTree(c.path));
return root;
}
在这段代码中,使用了 Files.walkFileTree
方法来遍历文件树。首先创建了一个名为 root
的 PathTree
对象,然后使用 Files.walkFileTree
方法遍历文件树并执行名为 labda_add_children_to_root_above
的操作。接着,对于每个子节点,使用递归调用 createTree
方法来构建子树。最后,返回根节点 root
。
英文:
you can write a recursive method using
walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) Walks a file tree.
pseudo code
PathTree createTree(String root) {
PathTree root = new PathTree(root)
Files.walkFileTree(root, options, labda_add_children_to_root_above)
root.children.foreach(c -> createTree(c.path))
return root;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论