• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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

Hi

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)

thanks
ryno71
0
ryno71
Asked:
ryno71
  • 4
  • 2
  • 2
  • +2
2 Solutions
 
sciuriwareCommented:
String filename;

if(new File(filename).exists)
{
........................
}

;JOOP!
0
 
sciuriwareCommented:
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");  ).
;JOOP!
0
 
CEHJCommented:
Equivalent to Unix find would normally involve recursion. This is how to do it platform-independently in pure Java.


http://javaalmanac.com/egs/java.io/TraverseTree.html

NB. method process(File) needs to be supplied by you
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

;JOOP!
0
 
hoomanvCommented:
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)
            System.out.println(list[0]);
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 ryno.data, 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.

Thanks
ryno71
0
 
CEHJCommented:
Using a filter is actually not efficient. You can just use File.exists for each case you have
0
 
objectsCommented:
> 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();
names.add("ryno.data");
names.add("app71.txt");
names.add("day.obj");
File[] files = dir.listFiles(new MyFilter(names));
0
 
sciuriwareCommented:
ryno71, why can't you just use my simple code 3 times, as you are looking for 3 files.
;JOOP!
0
 
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!
ryno71
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now