簡體   English   中英

遍歷文件樹而不進行遞歸和堆棧Java

[英]Traverse a file tree without recursion and with stack Java

請問,如何在沒有遞歸的情況下遍歷文件樹/目錄,以及如何使用Java遍歷堆棧。

public void traverse(Path path)
throws IOException
{
    Stack<Stream<Path>> st = new Stack<>();
    st.add(Files.list(path));
    for(Iterator<Path> it = st.peek().iterator(); it.hasNext(); )
    {
        Path temp = it.next();
        final BasicFileAttributes fa = Files.readAttributes(temp, BasicFileAttributes.class);
        if(fa.isDirectory())
        {
            //list all the directory contents
            st.push(Files.list(temp));
        }
        else if(fa.isRegularFile())
        {
        }
        else if(fa.isSymbolicLink()) {} //symbolic link
        else if(fa.isOther()) {} //other
        else {}
    }
}

謝謝!

基本上你有一棵路徑樹。 像任何其他樹一樣遍歷它。 他們提供了一個很好的例子迭代,堆棧的協助下,有序,二叉樹的遍歷這里

嘗試擴展它。

所有堆棧都提供了一些“內存”,告訴您來自哪里,這樣當您無法在當前分支中找到所需內容時,您可以返回樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM