Find and count files of specified name or extension

import java.io.*;
class CountFileFolder
{
    static String searchFile=".mp3";
    static int count=0;
    public static void main(String ar[])throws IOException
    {
        long s=System.currentTimeMillis();
        search("F:/");
        long e=System.currentTimeMillis();
        long total=e-s;
        double d=total/1000;
        System.out.println("Time consumed = "+d+" sec");
        System.out.println("Found = "+count+" files");
    }
    
    public static void search(String dir)throws IOException
    {
        File fil = new File(dir);
        File flist[]= fil.listFiles();
        for(File s:flist)
        {
            //System.out.println(s);
            if(s.getName().contains(searchFile)){
            
                System.out.println("File found at = "+s.getAbsolutePath());
                count++;
            }
            else if(s.isDirectory()){
                //System.out.println("Searching in:"+s.getName());
                search(s.getPath());
            }
        }
        
    }
    

No comments:

Popular Posts