Java: using recursion to read a folder and its content in tree format sub-folders/files in tree format

Java CODE: Using recursion to read a folder and its content - sub-folders/files in tree format.

public class TreeTest {
      public static void main(String[] args) {
            showDir(1, new File("D:\\test"));
      }

      static void showDir(int indent, File file) {
            for (int i = 0; i < indent; i++)
                  System.out.print(' ');
            System.out.println(file.getName());
            if (file.isDirectory()) {
                  File[] files = file.listFiles();
                  for (int i = 0; i < files.length; i++)
                        showDir(indent + 4, files[i]);
            }
      }
}

No comments:

Post a Comment

Your Comment and Question will help to make this blog better...