Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用递归和堆栈Java遍历文件树_Java_Path - Fatal编程技术网

不使用递归和堆栈Java遍历文件树

不使用递归和堆栈Java遍历文件树,java,path,Java,Path,请告诉我,如何使用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(); )

请告诉我,如何使用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 {}
    }
}
public void遍历(路径)
抛出IOException
{
Stack st=新堆栈();
st.add(Files.list(path));
for(Iterator it=st.peek().Iterator();it.hasNext();)
{
Path temp=it.next();
final BasicFileAttributes fa=Files.readAttributes(temp,BasicFileAttributes.class);
if(fa.isDirectory())
{
//列出所有目录内容
st.push(文件列表(临时));
}
else if(fa.isRegularFile())
{
}
else if(fa.isSymbolicLink()){}//符号链接
else如果(fa.isOther()){}//other
else{}
}
}

谢谢

基本上你有一个路径树。像其他树一样穿过它。 它们为二叉树的迭代、堆栈辅助顺序遍历提供了一个很好的示例

试着扩展它


堆栈所做的只是为您提供一些关于您来自何处的“内存”,以便在当前分支中找不到所需内容时可以返回树

基本上你有一个路径树。像其他树一样穿过它。 它们为二叉树的迭代、堆栈辅助顺序遍历提供了一个很好的示例

试着扩展它

堆栈所做的只是为您提供一些关于您来自何处的“内存”,以便在当前分支中找不到所需内容时可以返回树