2017-04-23 6 views
0

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

ありがとうございます!

答えて

0

基本的にパスのツリーがあります。あなたが他の木と同じようにそれを横断する。 これらは、バイナリツリーの反復、スタックアシスト、順序通り、トラバーサルの良い例を提供しますhere

拡大してみてください。

スタックは、現在のブランチで必要なものを見つけられなかった場合に、ツリーのバックアップをとることができるように、あなたが来た場所の「メモリ」を提供します。

関連する問題