0
请学习如何遍历一个没有递归的文件树/目录以及使用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 {}
}
}
谢谢!