Saturday, 31 August 2019

[Java] Print all the files under a given directory

Print all the files under a given directory. If there are subdirectories traverse them recursively and figure out all the files inside it.

package test.test;

import java.io.File;

public class Test {
   public static void main(String[] args) {
      filePrintUtil("/Users/poddepally/Downloads");
   }

   private static void filePrintUtil(String pathName) {
      File file = new File(pathName);
      File[] listFiles = file.listFiles();
      for (File currentFile : listFiles) {
          if (currentFile.isFile()) {
              System.out.println(currentFile.getName()
                  + ", length(bytes) =" + currentFile.length()
                  + ", parent = " + currentFile.getParentFile().getName());
   } else {
       filePrintUtil(currentFile.getAbsolutePath());
   }
      }
   }
}

No comments:

Post a Comment