?
Solved

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

Posted on 2006-04-22
10
Medium Priority
?
225 Views
Last Modified: 2010-03-31
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
Comment
Question by:ryno71
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 16516095
String filename;

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

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16516104
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16516142
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:sciuriware
ID: 16516184
The questioner did not ask for a "deeper" search!

;JOOP!
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 800 total points
ID: 16516528
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
 

Author Comment

by:ryno71
ID: 16520675
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16520701
Using a filter is actually not efficient. You can just use File.exists for each case you have
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 16521309
> 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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16522408
ryno71, why can't you just use my simple code 3 times, as you are looking for 3 files.
;JOOP!
0
 

Author Comment

by:ryno71
ID: 16525791
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question