help in search for a file...

hai...

i need to search my machine for a particular file and as a result get the path of that particular file...

tere is a recursive function for this.but the problem with this code is that it just searches a single folder and not all the folders of the drive. my code is

public String findFile(File f, String target)

{

if (f.getName().equals(target))

{

return f.getPath();

}

File[] list = f.listFiles();

for (short i = 0; i < list.length; i++)

{

if (list[i].isDirectory())

{

return findFile(list[i], target);

}

else

if (list[i].getName().equals(target))

return list[i].getPath();

}

return" Not found";

}

in this target is the file to be searched and the other parameter is the drive where the file is to searched...

plz help me...

[1477 byte] By [doubtsa] at [2007-10-2 18:26:48]
# 1

The order of your tests needs to change. Something along the lines of

public File findFile(File f, String target)

{

if (f.isDirectory())

{

File[] list = f.listFiles();

File foundFile = null;

for (int i = 0; i < list.length; i++)

{

foundFile = findFile(list[i], target);

if (foundFile != null)

break;

}

return foundFile;

}

else if (f.isFile() && f.getName().equals(target))

{

return f;

}

else

return null;

}

sabre150a at 2007-7-13 19:47:56 > top of Java-index,Java Essentials,Java Programming...
# 2
hai..thanks a lot...itz working well..thanks for ur help..bye
doubtsa at 2007-7-13 19:47:56 > top of Java-index,Java Essentials,Java Programming...