简体   繁体   中英

Find specific folder in Java

I am looking to find the path to a specific folder in Java as part of a bigger program.
What I have is a recursive function that checks every file that is contained within a starting directory and if it finds the folder I am looking for it assigns the path as a string to a variable. The following code works:

//root is the starting directory and name is the folder I am looking for
private void findDir(File root, String name)  
{  
    if (root.getName().equals(name))
    {
        toFind = root.getAbsolutePath();
    }

    File[] files = root.listFiles();

    if(files != null)
    {
        for (File f : files)  
        {
            if(f.isDirectory())
            {   
                findDir(f, name);
            }
        }
    }
}

This works but I do not like the fact that I have to use the 'toFind' variable. My question is is there a way to make the method return a String instead of void? This will also save the program from checking all the other files in the system after finding the one it is looking for.
I was thinking of something like this but the following code will return null even if it finds the folder.

private String findDir(File root, String name)
{
    if (root.getName().equals(name))
    {
        return root.getAbsolutePath();
    }

    File[] files = root.listFiles();

    if(files != null)
    {
        for (File f : files)  
        {
            if(f.isDirectory())
            {   
                return findDir(f, name);
            }
        }
    }

    return null; //???
}

This is because the first directory in your tree with no sub-directories will return null due to the fact that you specify that if the result for listFiles() is null , to return null for the entire recursion. It's not immediately obvious, but this can be fixed by changing the behavior in your for loop. Rather than directly returning a result inside your for loop, you should test if the result is null , and if so, just continue. If you however have a non-null result you can propagate the result upwards.

private String findDir(File root, String name)
{
    if (root.getName().equals(name))
    {
        return root.getAbsolutePath();
    }

    File[] files = root.listFiles();

    if(files != null)
    {
        for (File f : files)  
        {
            if(f.isDirectory())
            {   
                String myResult = findDir(f, name);
                //this just means this branch of the
                //recursion reached the end of the
                //directory tree without results, but
                //we don't want to cut it short here,
                //we still need to check the other
                //directories, so continue the for loop
                if (myResult == null) {
                    continue;
                }
                //we found a result so return!
                else {
                    return myResult;
                }
            }
        }
    }

    //we don't actually need to change this. It just means we reached
    //the end of the directory tree (there are no more sub-directories
    //in this directory) and didn't find the result
    return null;
}

Edit : Using Boris the Spider's suggestion, we could actually strip down the if statement to avoid the somewhat clunky nature of the continue statement, and make the code a little more to-the-point. Instead of:

if (myResult == null) {
    continue;
}
else {
    return myResult;
}

we could just slip in its place:

if (myResult != null) {
    return myResult;
}

which will evaluate with the same exact logic and takes less overall code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM