Sunday, 25 June 2017

Search File in Java Recursively ~ foundjava

An example recursive logic on searching file like windows search in Java. This logic is much similar to the logic used in deleting folder in Java

Search File in Java - Program


import java.io.*;
import java.util.*;
class JavaSearchFile
{
static Vector<File> v;

 public static void main(String args[]) throws Exception
 {

 // Create a file pointing a folder
 File f=new File(args[0]);
 
 // Create a vector of found files
 v=new Vector<File>();
 
 // Print the search starting
 System.out.println("\nStarting search....\n-------------------------\n");
 
 // Go search args[1] (2nd arg) in dir f
 search(f,args[1]);
 
 // Print the found files
 print();
 
 }

 public static void search(File file,String name)
 {
 

 // Print where the search is going on..
 System.out.println("Searching in "+file.getAbsolutePath());

  // Check if file is directory/folder
  if(file.isDirectory())
  {


   if(file.getName().contains(name))
   {
   
   // Add the file to found files vector
   v.addElement(file);
   
   }

  // Get all files in the folder
  File[] files=file.listFiles();


   for(int i=0;i<files.length;i++)
   {
   try
   {

    if(files[i].isDirectory())
    {
    
    // Go search for files if dir
    search(files[i],name);
    
    }

    else
    {

     if(files[i].getName().toLowerCase().contains(name.toLowerCase()))
     {
     
     // Add the found file to vector
     v.addElement(files[i]);
     
     }
   
    }
    
   }catch(Exception e){}
   
   }

  }
 
 }

 public static void print()
 {
 
 // Create a file array of v size
 File[] f=new File[v.size()];
 
 // Copy vector data into f
 v.copyInto(f);

 // Print the results
 System.out.println("\nResults");
 System.out.println("------------------------------------------");
 
  // Loop till end of size
  for(File k:f)
  {
  
  // Print the file path
  System.out.println("Found at "+k.getAbsolutePath());
  
  }
 
 }

}

Sample Output


Starting search....
-------------------------

Searching in f:\songs
Searching in f:\songs\A R Rahman - Superheavy
Searching in f:\songs\Agneepath
Searching in f:\songs\Aisha
Searching in f:\songs\Alex Clare
Searching in f:\songs\Anandam
Searching in f:\songs\Anandam\covers
Searching in f:\songs\Andala Rakshasi
Searching in f:\songs\Anjaana Anjaani
Searching in f:\songs\Annadammula Anubandham
Searching in f:\songs\Annadammula Anubandham\artwork
Searching in f:\songs\Awaara
Searching in f:\songs\Barfi!
Searching in f:\songs\Bharateyudu
Searching in f:\songs\Bodyguard
Searching in f:\songs\Brothers (2012)
Searching in f:\songs\Cameraman Ganga Tho Rambabu
Searching in f:\songs\Cheli
Searching in f:\songs\Dabangg 2
Searching in f:\songs\Damarukam
Searching in f:\songs\Delhi 6
Searching in f:\songs\Devudu Chesina Manushulu
Searching in f:\songs\Dhenikaina Ready
Searching in f:\songs\Emraan Hits
Searching in f:\songs\Emraan Hits\covers
Searching in f:\songs\English
Searching in f:\songs\Enrique Iglesias
Searching in f:\songs\fifa
Searching in f:\songs\Flo Rida
Searching in f:\songs\Fun
Searching in f:\songs\Gabbar Singh
Searching in f:\songs\Gangnam Style (?????)
Searching in f:\songs\Ghajini
Searching in f:\songs\Ghatikudu
Searching in f:\songs\Gotye
Searching in f:\songs\i hate love stories
Searching in f:\songs\Jaane Tu Ya Jaane Na
Searching in f:\songs\Jab Tak Hai Jaan
Searching in f:\songs\Jhootha Hi Sahi
Searching in f:\songs\Johnny
Searching in f:\songs\Julayi
Searching in f:\songs\Justin Bieber
Searching in f:\songs\KIKK
Searching in f:\songs\Krishnam Vande Jagadgurum
Searching in f:\songs\Linkin Park - Project Revolution
Searching in f:\songs\Love To Love
Searching in f:\songs\Love To Love\Love To Love (2012) ~128Kbps
Searching in f:\songs\Maroon 5
Searching in f:\songs\Melody
Searching in f:\songs\Mr. Perfect
Searching in f:\songs\Nene Ambani
Searching in f:\songs\Ninnu Choosthe Love Vasthundi
Searching in f:\songs\Nirantharam Nee Oohale
Searching in f:\songs\Ok Ok
Searching in f:\songs\Orange
Searching in f:\songs\Orange\artwork
Searching in f:\songs\Other Stuff
Searching in f:\songs\Pitbull
Searching in f:\songs\Power
Searching in f:\songs\Prema Desam
Searching in f:\songs\Premikudu
Searching in f:\songs\Premikula Roju
Searching in f:\songs\Raghavan
Searching in f:\songs\Routine Love Story
Searching in f:\songs\Sakhi
Searching in f:\songs\Sarocharu
Searching in f:\songs\Surya Krishnan
Searching in f:\songs\Surya Krishnan\Surya SO Krishnan-320kbps
Searching in f:\songs\Talaash
Searching in f:\songs\Taylor Swift
Searching in f:\songs\Teri Meri Kahaani
Searching in f:\songs\The Official UK Top 40 Singles Chart 15-07-2012
Searching in f:\songs\Thuppakki
Searching in f:\songs\Unnale Unnale
Searching in f:\songs\Vaasu_2002_320VBR
Searching in f:\songs\Vaasu_2002_320VBR\Vaasu (2002) ~ 320 VBR
Searching in f:\songs\Vicky Donor
Searching in f:\songs\Vicky.Donor
Searching in f:\songs\Vilan
Searching in f:\songs\Viswaroopam
Searching in f:\songs\Westlife
Searching in f:\songs\Yeto Vellipoyindhi Manasu

Results
------------------------------------------
Found at f:\songs\English\Whistle.mp3
Found at f:\songs\The Official UK Top 40 Singles Chart 15-07-2012\06 Flo Rida -
Whistle.mp3

No comments:

Post a Comment