Java示例显示目录中的目录


Java示例显示目录中的目录

import java.io.File;

import java.io.IOException;



public class FileDisplay {

   public static void main(String[] args) {

      File currentDir = new File(".");

      displayDirectoryContents(currentDir);

   }

   public static void displayDirectoryContents(File dir) {

      try {

         File[] files = dir.listFiles();

         for (File file : files) {

            if (file.isDirectory()) {

               System.out.println("directory:" + file.getCanonicalPath());

               displayDirectoryContents(file);

            } else {

               System.out.println("     file:" + file.getCanonicalPath());

            }

         }

      } catch (IOException e) {

         e.printStackTrace();

      }

   }

}