Java recursive file search

I need help with a method to find a file given a specific parent directory. It needs to take in the parent directory and file to find as arguments, and return the full path to the file or an empty string if the file was not found. Here is what I have so far, it doesn't work :(.

private String findFile(File parentDir, File fileToFind)

{

File[] filesAndDirs = parentDir.listFiles();

for( File file: filesAndDirs )

{

if( file.getName().equals(fileToFind.getName()) )

{

return file.getAbsolutePath();

}

if (!file.isFile())

{

return findFile(file, fileToFind);

}

}

return"";

}

[1142 byte] By [Losinga] at [2007-11-27 4:48:37]
# 1
!file.isFile() != file.isDirectory()Aside from that, "it doesn't work" is an insufficient description of the problem. ~
yawmarka at 2007-7-12 10:01:34 > top of Java-index,Java Essentials,Java Programming...
# 2

if (!file.isFile())

{

return findFile(file, fileToFind);

}

This might not work very well either. If you make a call here, and this goes down the list and doesn't find anything, the method will return back, and you might not have necessarily gone through all the files.

Maybe something like this

if (file.isDirectory()) {

String name = fineFile (file, fileToFind);

if (!name.equals("")) {

return name;

}

}

kdajania at 2007-7-12 10:01:34 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for the help, seems to be working well now. I love you all.
Losinga at 2007-7-12 10:01:34 > top of Java-index,Java Essentials,Java Programming...