Java program to List all files in a directory and nested sub-directories



import java.io.*;
  import java.util.*;
  class solution
  {
    static void yRec(File[] arr,int index , int level){
      if(index==arr.length )
        return;
      for(int i=0;i level;i++)
        System.out.print("\t");
      if(arr[index].isFile())
      {
        System.out.println("File : "+arr[index].getName()+" of size : "+arr[index].length()+" bytes");
      }
      else if(arr[index].isDirectory()){
        File[] subArr = arr[index].listFiles();
        if(subArr.length == 0)
        	return;
        System.out.println("[" + arr[index].getName() + "]" + " has "+subArr.length+" files. Path : "+subArr[0].getParent());
        yRec(subArr,0,level+1);
      }
      yRec(arr,++index,level);
    }
    public static void main(String[] args) {
      String path = "D:\\web\\assets\\fonts";
      // String path = args[0];
      File mainDir = new File(path);
      if(mainDir.isDirectory() && mainDir.exists())
      {
        File arr[] = mainDir.listFiles();
        System.out.println("------------------");
        System.out.println("Files and folders of "+path);
        System.out.println("------------------");
        yRec(arr,0,0);
      }
    }
  }
  

Output :

------------------
Files and folders of D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts
------------------
[fontawesome] has 8 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\fontawesome
        [css] has 14 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\fontawesome\css
                File : all.css of size : 70523 bytes
                File : all.min.css of size : 56842 bytes
                File : brands.css of size : 714 bytes
                File : brands.min.css of size : 661 bytes
                File : fontawesome.css of size : 68908 bytes
                File : fontawesome.min.css of size : 55415 bytes
                File : regular.css of size : 733 bytes
                File : regular.min.css of size : 676 bytes
                File : solid.css of size : 726 bytes
                File : solid.min.css of size : 668 bytes
                File : svg-with-js.css of size : 8077 bytes
                File : svg-with-js.min.css of size : 6359 bytes
                File : v4-shims.css of size : 41218 bytes
                File : v4-shims.min.css of size : 26626 bytes
        File : fontawesome-webfont.eot of size : 165742 bytes
        File : fontawesome-webfont.svg of size : 447050 bytes
        File : fontawesome-webfont.ttf of size : 165548 bytes
        File : fontawesome-webfont.woff of size : 98024 bytes
        File : fontawesome-webfont.woff2 of size : 77160 bytes
        File : FontAwesome.otf of size : 134808 bytes
        [webfonts] has 15 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\fontawesome\webfonts
                File : fa-brands-400.eot of size : 130906 bytes
                File : fa-brands-400.svg of size : 700503 bytes
                File : fa-brands-400.ttf of size : 130600 bytes
                File : fa-brands-400.woff of size : 88428 bytes
                File : fa-brands-400.woff2 of size : 75336 bytes
                File : fa-regular-400.eot of size : 34394 bytes
                File : fa-regular-400.svg of size : 144452 bytes
                File : fa-regular-400.ttf of size : 34096 bytes
                File : fa-regular-400.woff of size : 16804 bytes
                File : fa-regular-400.woff2 of size : 13584 bytes
                File : fa-solid-900.eot of size : 192758 bytes
                File : fa-solid-900.svg of size : 842605 bytes
                File : fa-solid-900.ttf of size : 192472 bytes
                File : fa-solid-900.woff of size : 98384 bytes
                File : fa-solid-900.woff2 of size : 75728 bytes
[simple-line-icons] has 5 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\simple-line-icons
        File : Simple-Line-Icons.eot of size : 54266 bytes
        File : Simple-Line-Icons.svg of size : 239045 bytes
        File : Simple-Line-Icons.ttf of size : 54056 bytes
        File : Simple-Line-Icons.woff of size : 81332 bytes
        File : Simple-Line-Icons.woff2 of size : 30064 bytes
[slick] has 4 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\slick
        File : slick.eot of size : 2048 bytes
        File : slick.svg of size : 2152 bytes
        File : slick.ttf of size : 1892 bytes
        File : slick.woff of size : 1380 bytes
[star] has 4 files. Path : D:\installed programs\xamppinstallation\htdocs\affiliateClient\wordpress\wp-content\themes\oceanwp\assets\fonts\star
        File : star.eot of size : 1676 bytes
        File : star.svg of size : 1391 bytes
        File : star.ttf of size : 1512 bytes
        File : star.woff of size : 1304 bytes

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java