Link to home
Start Free TrialLog in
Avatar of renisha
renisha

asked on

searching for a file

How to serach for a file name ,if someimes path also given ,also if metacharcters like * also given from the root directory itself?
Can threads be used to increase its effeciency?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this may also help:
http://www.javaworld.com/javaworld/javatips/jw-javatip93.html

Here's also some other code that adds a filter:

class SearchAndFilter implements FilenameFilter {

  public static final String SearchTxt = "Tyra";

  public static boolean accept(File dir, String name) {
    return
      name != null && name.startsWith(SearchExt);
  }

  public static Vector search(File dir, boolean recursive) {

     Vector v = new Vector();

     Files files[] = dir.listFiles();
     if (files == null) return v;
     for (int i=0; i<files.length; i++) {
       if (files[i].isFile())
         if (accept(dir, files[i].getName())
            v.add(files[i]);
       else {
         if (recursive && dir != null)
           v.add(search(files[i]));
       }

     }
   
     }
     return v;
  }

  public static final void main(String args[]) {
    File f = new File("c:\xyz\");
    Vector tyraBanksFiles = search(f, true);
  }
}

Threads won't really help you.
>> Can threads be used to increase its effeciency? ...

If the searches are likely to be longish, then threads would help improve the programme's usability.
Avatar of sciuriware
sciuriware

Threads will only help you (as krakatoa said) when, for instance, the search is very long.
In that case you might want to give a user the chance to abort.
So, you can put the search in a thread AT A LOW PRIORITY!, and stand ready to kill it from a button
or a timeout.
As objects mentioned, your program wil not go faster, even a bit slower.
And the results from a thread must be handled (when displayed life) by thread-safe code.
so, keep it simple when you can.
;JOOP!
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-- Points for objects

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer