To check for files and file types in a folder using Java

This example demonstrates the way to find the number files and file types in a folder.

import java.io.File;
public class DemoFile
{
     static void RecursivePrint(File[] arr,int index,int level)
     {
         // terminate condition
         if(index == arr.length)
             return;
         // tabs for internal levels
         for (int i = 0; i < level; i++)
             System.out.print(“\t”);
         // for files
         if(arr[index].isFile())
             System.out.println(arr[index].getName());
         // for sub-directories
         else if(arr[index].isDirectory())
         {
             System.out.println(“[” + arr[index].getName() + “]”);
             // recursion for sub-directories
             RecursivePrint(arr[index].listFiles(), 0, level + 1);
         }
         // recursion for main directory
         RecursivePrint(arr,++index, level);
    }
    // Driver Method
    public static void main(String[] args)
    {
        String maindirpath = “C:\\Users\\Desktop\\Demo”;
        File maindir = new File(maindirpath);
        if(maindir.exists() && maindir.isDirectory())
        {
            File arr[] = maindir.listFiles();
            System.out.println(“**********************************************”);
            System.out.println(“Files from main directory : ” + maindir);
            System.out.println(“**********************************************”);
                     RecursivePrint(arr,0,0);
       }
    }
}