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 {}
}
}
謝謝!