如何获取目录和子目录,并按父目录和子目录进行分组。

huangapple go评论86阅读模式
英文:

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 = &quot;/Users/user/Desktop/testFolder&quot;;

try (Stream&lt;Path&gt; paths = Files.walk(Paths.get(rootFolder))) {			
			List&lt;String&gt; pathStr = paths.filter(Files::isDirectory)
					.map(path -&gt; 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 : &quot;/Users/user/Desktop/testFolder&quot;,
   children : 
   [
      {
         path : &quot;/Users/user/Desktop/testFolder/folder1&quot;,
         children : 
         [
            {
               path : &quot;/Users/user/Desktop/testFolder/folder1/folder1&quot;,
               children : []
            }
         ]
      },
      {
         path : &quot;/Users/user/Desktop/testFolder/folder2&quot;,
         children : 
         [
            {
               path : &quot;/Users/user/Desktop/testFolder/folder2/folder1&quot;,
               children : []
            }
         ]
      },
      {
         path : &quot;/Users/user/Desktop/testFolder/folder3&quot;,
         children : 
         [
            {
               path : &quot;/Users/user/Desktop/testFolder/folder3/folder1&quot;,
               children : 
               [
                  {
                     path : &quot;/Users/user/Desktop/testFolder/folder3/folder1/folder1&quot;,
                     children : []
                  }
               ]
            }
         ]
      }   
   ]
}

I have the following class ready :

public class PathTree {
	private String path;
	private List&lt;PathTree&gt; 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 方法来遍历文件树。首先创建了一个名为 rootPathTree 对象,然后使用 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 -&gt; createTree(c.path))
    return root;
}

huangapple
  • 本文由 发表于 2020年8月25日 11:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63571705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定