Best Way using Java to see if a file/file type exists in a directory (platform independant ie not using unix find command)


What would be the best way using Java to see if a file/file type exists in a directory (platform independant ie not using unix find command)

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

String filename;

if(new File(filename).exists)

The 'filename' can be a full pathname (e.g. "/usr/lib/ghr/xyz.c") or a relative path (e.g. "../there/pqr.c"),
but in the latter case, you must know what your current directory is ( System.getProperty("user.dir");  ).
Equivalent to Unix find would normally involve recursion. This is how to do it platform-independently in pure Java.

NB. method process(File) needs to be supplied by you
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

The questioner did not ask for a "deeper" search!

you can use FilenameFilter to check for specific file names
the below program prints the first java file in the current directory or nothing if there is no java file

      File dir = new File(".");
      String[] list;
      FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                  return name.endsWith(".java");

      list = dir.list(filter);
      if(list.length > 0)
ryno71Author Commented:
So I would have to create a filter for each of the files types I was looking for then.  Say I was looking for, app71.txt, and day.obj... but they are all in the same directory as I placed them there.

Sorry, just mentioned find (unix) as I figured someone would say use that... but I want a java way. :)
All the files I would look for would be in one directory where I placed them...  but other have access to the directory which is why I need something that checks for the different type if they are there or not.

Using a filter is actually not efficient. You can just use File.exists for each case you have
Mick BarryJava DeveloperCommented:
> So I would have to create a filter for each of the files types I was looking for then.

Your filter would keep a list of files it was interested in:

     public class MyFilter implements FileFilter() {

          private Set accepted = null;

          public MyFilter(Set name) {
             accepted = names;

          public boolean accept(File file) {
               return accepted.contains(file.getName()) && file.exists();

Set names = new HashSet();
File[] files = dir.listFiles(new MyFilter(names));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ryno71, why can't you just use my simple code 3 times, as you are looking for 3 files.
ryno71Author Commented:
Actually Sciuriware, I could be looking for 3 to 38 files depending...

If it was just 3 files you are right using file.exists would be the easiest...

thanks for the help guys!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.