Blog der Heimetli Software AG

Directory traversal mit walkFileTree

Zwei Beispielprogramme zum traversieren eines Directory-Trees habe ich schon länger ins Netz gestellt: Rekursive Traversierung und Level-Order Traversierung.

Neuerdings bietet die Java-Library schon selber eine Traversierung an, mit der Methode walkFileTree in der Files-Klasse.

Diese Methode will ein Interface namens FileVisitor als Argument. Bei jedem File und Directory wird ein Callback in diesem Interface aufgerufen.

Zur Vereinfachung gibt es auch eine Klasse die dieses Interface imlementiert: SimpleFileVisitor.

Der folgende Code zeigt auf, wie mit Hilfe dieser Klassen und Methoden ein Directory traversiert wird.

Das Beispiel enthält zwei Klassen, Visitor und WalkFileTree. Darüber lässt sich bestimmt prima streiten, aber ich wollte alles in einem File haben und trotzdem nicht alles in einer einzigen Klasse.

/******************************************************************************/
/*                                                                            */
/*                                                    FILE: WalkFileTree.java */
/*                                                                            */
/*  Directory traversal using walkFileTree                                    */
/*  ======================================                                    */
/*                                                                            */
/*  V1.00   12-JUL-2014   Te          https://www.heimetli.ch                 */
/*                                                                            */
/******************************************************************************/

import java.io.* ;
import java.nio.file.* ;
import java.nio.file.attribute.* ;

/**
 * The visitor for the files and directories.
 *
 * It extends SimpleFileVisitor. SimpleFileVisitor
 * implements the interface FileVisitor which is
 * required by walkFileTree.
 */
class Visitor extends SimpleFileVisitor<Path>
{
   /**
    * This method prints the name and path of the file.
    *
    * @param path Name and path of the file
    * @param attr Attributes of the file
    */
   @Override
   public FileVisitResult visitFile( Path path, BasicFileAttributes attr ) throws IOException
   {
      System.out.println( path ) ;

      return FileVisitResult.CONTINUE ;
   }

   /**
    * This method prints the name and path of the directory.
    * Remove it if you want only the file names.
    *
    * @param path Name and path of the file
    * @param attr Attributes of the file
    */
   @Override
   public FileVisitResult preVisitDirectory( Path path, BasicFileAttributes attr ) throws IOException
   {
      System.out.println( path ) ;

      return FileVisitResult.CONTINUE ;
   }
}

/**
 * This is the main class which starts the directory
 * traversal.
 */
public class WalkFileTree
{
   /**
    * main calls walkFileTree and passes the obect
    * that implements FileVisitor.
    *
    * @param args Ignored
    */
   public static void main( String[] args )
   {
      try
      {
         Files.walkFileTree( Paths.get("."), new Visitor() ) ;
      }
      catch( IOException e )
      {
         e.printStackTrace() ;
      }
   }
}
WalkFileTree.java herunterladen

Noch fast einfacher geht es mit Java 8: Directory Traversal mit walk und Stream