Java Directory list content recursively

Description

Java Directory list content recursively

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
   public static void main(String args[]) throws Exception {
      printDir("F:");
   }//  www.  j av a2 s  .co m

   public static void printDir(String path) throws Exception {
      Path dir = Paths.get(path);
      DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
      for (Path p : stream) {
         System.out.println(p.toAbsolutePath());
         if (Files.isDirectory(p))
            printDir(path + "/" + p.getFileName());
      }
   }
}



PreviousNext

Related