In Java how do I create a structured tree using FileVisitor

泄露秘密 提交于 2020-01-06 03:31:06

问题


Given a starting path I want to create a tree like object representing the filesystem using Java, showing all the folders down to level x but not ordinary files. . So using FileVisitor I know that every time just before Im going to browse a new subfolder that the preVisitDirectory() method will be called and once it had completed parsing all its children the postVisitDirectory() will be called, but my problem is knowing how to attach this directory to its parent.

i.e in my case I want to create data for jstree using ul/li/ul/li elements, and Im doing this with j2html lib. So create root using ul(), then when I go into preVisitDirectory() I would create a li() element and in postVisitDirectory() would want to attach to ul() using ul().with(li) but I cant see how to keep track of where I am in building my tree.

e.g static hard coded example not actually browsing tree

public Tag createBrowseTreeAsHtml()
    {
        Tag ulTag = ul(
                li("ChildNode 2").withId("child_node_1"),
                li("ChildNode")
        );

        Tag divTag= div(
                    ul(
                        li("Root Node 1").with(ulTag),
                        li("Root Node 2")
                    )
                )
                .withId("jstree");
        return div().with(divTag);
    }

I see Guava has support for Graphs, should I be utilising this somehow ?

来源:https://stackoverflow.com/questions/48369728/in-java-how-do-i-create-a-structured-tree-using-filevisitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!